#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12

    clearing array elements?


    I have a character array:

    test[10];

    slots 0-9 are filled in the array..I want to clear the array so brand new character can be entered in there place. How would I go about doing this?

    -andy
    hmmm...
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,214
    Rep Power
    2222
    Two ways immediately come to mind.

    To write over all the elements of the array with a NULL (value of zero), you could either use a loop or use memset:
    Code:
    memset(test, 0, 10);
    Or, if you just want to make it an empty string:
    Code:
    test[0] = '\0';
    In this second case, it doesn't matter that the previous characters are still in the array, because the string ends at the NULL.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    third: just enter the new char's in the array. They don't mind if there's a '\0' there or some other char.
    Last edited by 7stud; May 10th, 2003 at 02:35 AM.

IMN logo majestic logo threadwatch logo seochat tools logo