clearing a variable

This is a discussion on clearing a variable within the C Programming forums, part of the General Programming Boards category; hello, i am writing a program in C. i have it like this so far: ------------------------------------ #include<stdio.h> char *com[256]; int ...

  1. #1
    Unregistered
    Guest

    Question clearing a variable

    hello,

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

    ------------------------------------
    #include<stdio.h>

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > 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
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    And if you wanted to simply blank out the array, this would do the trick:
    Code:
    memset( com, 0, sizeof(com) );
    Jason Deckard

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    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
    Unregistered
    Guest

    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, 03:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21