Thread: Initializing Arrays?

  1. #1
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48

    Question Initializing Arrays?

    how can I erase a array?

    Code:
    char array[150];
    
    loopA{
       loopB{
           do work over array;
       }
       //I need to erase his value to the next loop
       array[] = ""; //didn't work!
       }
    what the syntax? I can't find anywere... I only find when we declare the array.

  2. #2
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    i think you should use pointers for this
    Code:
    char *array=malloc(150 *sizeof(int));
    //do your work then
    free(array);

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    There's no such thing as "erase". Do you mean set all the elements of the array to 0? Try this:
    Code:
    memset(array, 0, sizeof(*array) * sizeof(array));
    Make sure include the appropriate header for memset().
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Location
    Rio de Janeiro, Brasil
    Posts
    48
    converting to a pointer, I'll be able to do something like this?

    Code:
    char *array=malloc(150 *sizeof(int));
    char HStr[2000];
    
               while ((HStr[j] != '}') || (HStr[j]=='\0')){ 
                   array[i] = HStr[j];
                   i++;j++;
               }
    thanks!

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    An array's size given by sizeof(), if it's an array, is the size in bytes. Therefore, the memset() example given above is incorrect. If array is a real array, then it'll setting too much memory. Otherwise, if it's just a pointer to a malloc()'ed block of memory, it stands a chance of setting too little memory.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    if you're using "array" as a null-terminated character array (which some would loosely refer to as a string), then all you need to do is set the first character to null:
    Code:
    array[0] = '\0';
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initializing arrays in a class
    By ejohns85 in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2009, 09:06 AM
  2. Replies: 23
    Last Post: 12-06-2008, 01:39 PM
  3. initializing multi-dimensional arrays
    By cyberfish in forum C++ Programming
    Replies: 4
    Last Post: 10-31-2007, 08:15 PM
  4. newbee: initializing arrays
    By breaka in forum C Programming
    Replies: 11
    Last Post: 06-12-2006, 12:20 PM
  5. Initializing dynamic arrays
    By cunnus88 in forum C++ Programming
    Replies: 9
    Last Post: 11-21-2005, 09:50 AM