Thread: question about arrays

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    84

    question about arrays

    i was wondering how i could reset an array, I have a character array that i need to reuse over and over again in a funciton, so I would like to blank it out I've been trying things like

    Code:
    word[] = {}
    
    word = { '\0'}
    But I just get error messages, I just want it to be empty so i can use it again

    thank you

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    i mean....
    Code:
    word [] = {'\0'}

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Use the memset function or a for loop for each element. You can't zero out an array with = except at initalisation.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    84
    thanks for the tip, i've never used memset before, i looked it up but don't understand how to implement it

    the definition I found looks like this

    void *memset(void *s, int c, size_t n);

    i've tried

    Code:
    memset(word,Max_size);
    and different variations of that, but get errors

    thank you

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    from the definition you can see that memset takes 3 parameters, but you are passing it two.

    You seem to be passing the first and third parameters, but not the second. The second is the value. In the case of initalising to 0:
    Code:
    memset(word, 0, Max_size);

  6. #6
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Quote Originally Posted by panfilero
    i mean....
    Code:
    word [] = {'\0'}
    would set the string to zero length string.Incase you dont want to change all the bytes means if in a function you want to fill the array word by just checking if the word array is empty.Just setting the first will be efficient
    Code:
    word[0]='\0';
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    would set the string to zero length string.
    A zero-length string, but a 1 length array. Assuming a semicolon was added.

    BTW, memset() is in <string.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about arrays.
    By Kelvie in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2007, 05:32 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 08:24 PM
  3. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM