clearing array

This is a discussion on clearing array within the C Programming forums, part of the General Programming Boards category; hi all i have a question. if i have and array and have already initialize it as follow Code: unsigned ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    21

    clearing array

    hi all

    i have a question. if i have and array and have already initialize it as follow

    Code:
    unsigned char data[5] = {0};
    how do i clear it after i have used it?

    lets say
    Code:
    for (int a= 0; a<5;a++)
      data[a] = a;   //filling array with data
    
    //after filling it i want to clear the whole array
    
    data[X] = 0:  //is this the right way.What to put for X to clear the whole array?
    Thanks

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    When you declare an array on the stack, as you have in your original post, you do not have to worry too much about memory management. Your array was declared in the scope of a function, say main(). If there is room on the stack (usually yes) the array will be created and ready to use. When main() returns, the character array is out of scope and automatically deleted.

    If you want to re-use the character array, don't try clear it, just overwrite it.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Code:
    data[X] = {0};
    Pull my finger

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. That only "clears" one element. That initializer only works at the time of declaration. Otherwise you have to use a loop, or memset, which is just a loop wrapped in a function.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    By clearing you mean you want to zero the contents?
    You can use a loop just like you did above,
    Code:
    for (a = 0; a < 5; a++)
        data[a] = 0;
    Or you can use the memset function
    Code:
    #include <string.h>
    
    memset(data, 0, 5);
    (edit: oh no too late)

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Quote Originally Posted by quzah
    No. That only "clears" one element. That initializer only works at the time of declaration. Otherwise you have to use a loop, or memset, which is just a loop wrapped in a function.


    Quzah.
    Ughhhh... I keep forgetting initialization and assignments are two different beasts in c. Oh sweet lord, I think I'm having flashbacks of LISP. Ooohhhhh.... my aching hips.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    Quote Originally Posted by quzah
    No. That only "clears" one element. That initializer only works at the time of declaration. Otherwise you have to use a loop, or memset, which is just a loop wrapped in a function.


    Quzah.
    quick question: is it wrong to do something like:

    Code:
    data[0] = '/0';
    i know it doesn't _clear_ the elements, but if you are just going to overwrite them anyway would this be bad form?

    thanks.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,706
    Code:
    data[0] = '\0';
    If you wanted to overwrite the data, it would be bad form, because that smashes the array. The null character signals the end of the array, so that array ends as soon as it begins.

    Like I told everyone, just overwrite it. It doesn't have to be more complicated than
    Code:
     bigstr[80] = "I like rainbows, and puppies, and sunny days!";
    There are lots of c-string functions that aid you in doing whatever you please, like memset, memmove, strncpy, etc.
    Last edited by whiteflags; 04-26-2006 at 01:04 PM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No it doesn't. For an array it doesn't do anything other than set one element to the null character. The rest are left unchanged. It doesn't deallocate anything. The array size is still the same. It never changes.

    It would allow string functions to treat it as empty, assuming they're all ones that provide null termination. strncpy is one such function that may not always terminate with a null.

    In any event, setting the first element to null is common practice for treating a character array as empty. Just make sure your reuse of it provides said null termination.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by citizen
    It doesn't have to be more complicated than
    Code:
     bigstr[80] = "I like rainbows, and puppies, and sunny days!";
    Actually it does have to be more complicated than that. Your code doesn't rewrite the array filling it with a string. It tries to assign the index bigstr[80] with a string. Obviously this will fail. You can only fill a char array with a string like that when it is being declared.


    Code:
    char bigstr[80] = "I like rainbows, and puppies, and sunny days!";
    To overwrite the array with a new string, you need to use a function like strcpy().

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    21
    I see I see...thanks guy

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    If you just to clear the array why can't you just reinitialise it to zero?

    Code:
    anarray[5]={0,0,0,0,0};

  13. #13
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by hammer1234
    If you just to clear the array why can't you just reinitialise it to zero?

    Code:
    anarray[5]={0,0,0,0,0};
    You can't reinitialise it. Reinitialise... kind of an oxymoron isn't it?

  14. #14
    Registered User
    Join Date
    Feb 2006
    Location
    Great India
    Posts
    24
    Mr hammer1234 just tell me what will you do if the array size if 100 ?

    why not use the loop instead of writing each value ?


    REALNAPSTER

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    13
    Quote Originally Posted by realnapster
    Mr hammer1234 just tell me what will you do if the array size if 100 ?

    why not use the loop instead of writing each value ?


    REALNAPSTER

    Because Mr realnapster the person posting the code said the array has a size of 5.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. clearing a used array
    By smegly in forum C Programming
    Replies: 10
    Last Post: 05-22-2004, 02:42 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM

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