reinitialize a global variable array from within a function

This is a discussion on reinitialize a global variable array from within a function within the C Programming forums, part of the General Programming Boards category; How would I do this? I want the value of the variable to be returned to its initial value of: ...

  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
    ZuK
    ZuK is offline
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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, 06:18 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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