Thread: clearing a variable

  1. #1

    Question clearing a variable


    i am writing a program in C. i have it like this so far:


    char *com[256];

    int main()
    [first half of prog goes here]
    [i want to clear the com variable here]
    [last half here]

    return 0;

    yea, so can anyone tell me how i can clear the variable? cause its currently got the string "echo hello" in it, and i want to put in "echo yes", but it turns out as "echo yeslo" so i figured if i cleared it it wouldn't do that.

    thanks all!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > char *com[256];
    Given your description, I'm pretty sure you should be using

    char com[256];

    > but it turns out as "echo yeslo"
    Well if you did
    strncpy( com, "echo yes", 8 );
    that is what you would see

    So just do this
    strcpy( com, "echo yes" );

  3. #3
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    And if you wanted to simply blank out the array, this would do the trick:
    memset( com, 0, sizeof(com) );
    Jason Deckard

  4. #4
    Join Date
    Aug 2001
    Groningen (NL)
    Or simply use a for-loop to clear the array or just a part of it, since in both cases there will be "echo " in the string.

  5. #5

    Smile thanks!

    thanks alot guys, especially Deckard. i looked up the man page for memset, and it looks like a really cool function, and now my prog works

    thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  2. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM