Thread: reinitialize a global variable array from within a function

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    62

    reinitialize a global variable array from within a function

    How would I do this? I want the value of the variable to be returned to its initial value of: char x[MAX_DIR_LENGTH]={0x0};

    I've tried
    Code:
    x[MAX_DIR_LENGTH]='\0';
    x[MAX_DIR_LENGTH]={0x0};

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    memset( x, '\0', MAX_DIR_LENGTH );
    does the same as your initialisation.
    Kurt

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Or even just a regular for loop, which doesn't have the problem of figuring out the size of each element of the array.

    memset( x, '\0', MAX_DIR_LENGTH * sizeof *x );
    Will at least save embarrasment if your array isn't an array of chars

    For example, using memset to clear arrays of pointers and floats is non-portable.
    http://c-faq.com/malloc/calloc.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM