clearing an char array

This is a discussion on clearing an char array within the C++ Programming forums, part of the General Programming Boards category; how would i reset an char temp [30] array back to empty for instance, after a loop it has some ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    71

    clearing an char array

    how would i reset an char temp [30] array back to empty

    for instance, after a loop it has some string stored in it. i want to clear it.
    i could do it manually by going through a loop and setting each of its elements to 0 ascii
    but too annoying. is there like a built in function for that.
    thanks

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You can just call a handy function named "memset" which sets a specified memory range to a specified value. It works as follows.

    Code:
    char array[30];
    
    // ... some computations...
    
    // Now we clear our array
    memset( array, 0, sizeof(array) );
    Param 1: Pointer to destination
    Param 2: Character to set
    Param 3: Number of characters

    NOTE: There are a few things to note here. An array acts like a pointer so we don't need to have the address-of operator before the name.

    Here is another quick example to zero out the memory of a structure Foo..
    Code:
    Foo myStruct;
    
    memset( &myStruct, 0, sizeof(Foo) );
    It is located in the either one of these headers

    memory.h
    string.h

    Also if you are using windows.h they have a handy macro named "ZeroMemory" which simply takes the first and third parameter of memset.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    71
    oh thats right
    memset, i forgot about it
    well, is there a string related function?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Here is another quick example to zero out the memory of a structure Foo..
    Be careful doing that one, if the struct contains pointers or floats they could get messed up. This is covered here .
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    71
    Originally posted by Hammer
    >>Here is another quick example to zero out the memory of a structure Foo..
    Be careful doing that one, if the struct contains pointers or floats they could get messed up. This is covered here .

    yup, i agree with that

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you don't want to empty the complete char array, you can do this:
    myarray[0] = '\0';
    to give to impression that the array has been emptied.

    It depends on what you want to do...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    71
    nah, i want to empty it completely cuz i am going through a loop to keep reading names from a file and i dont wanna use up space
    so i wanna re use the char array

    i was hoping that there might be some C++ function which could do it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  3. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-03-2005, 12:15 AM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 12:44 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 12:57 PM

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