Clearing a 2D array

This is a discussion on Clearing a 2D array within the C Programming forums, part of the General Programming Boards category; Hi Guys, I have a simple problem with, what seems to me, should be a simple solution but it's just ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    13

    Clearing a 2D array

    Hi Guys,

    I have a simple problem with, what seems to me, should be a simple solution but it's just not working.

    I have a 2D array declared as
    Code:
    *array[20]
    This array is then dynmaically filled with 4 or 5 strings I then want to clear this array once I have finished with it.

    I have tried runnig it through a loop such as
    Code:
    for (i=0;i<totalnumberofstrings;i++)
    {
    array[i] = '\0'
    }
    I've also tried array[i] = 0, array[i] = NULL.

    Everything I have tried it's not effecting the values of the array and they won't clear.

    I assume it's not working because it is actually a 2D array, I'm just stuggling how to clear it.

    Thank you very much for any help!!

    Forg

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    *array[20] is not a 2D array but an array of pointers to char(s); can you post more of the relevant code.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    13
    That is correct. My array is full of strings. Sorry for my bad description. An example of my array is

    array[0] - hello
    array[1] - world

    how do I then go about clearing 'array' so they no longer hold those words.
    Thank you

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Depends on what you mean by "clearing". It is a 1D pointer array, so you could set the pointers to an empty string, perhaps, or NULL?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    13
    That would be fine. I just need array[0] and array[1] to no longer equal the string they were. It just needs to be cleared so it won't be read. Just stores random data or anything.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    In that case set each element of array[] to NULL; looking back at your first post you were doing that but were probably going out of bounds depending on the value of totalnumberofstrings.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    13
    I seem to pass totalnumberofstrings+1 and now it all works magically! wow. That means it works!! YAY!!! all these hours! and now it works!! That's why we all program, isn't it!!

    Thank you for the general talking inspriation!!!

    Many Thanks

    Forg

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    So perhaps all but the last one wasn't getting cleared until you passed it totalnumberofstrings+1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 04:45 AM
  3. 2D array becoming "deallocaded"
    By Aaron M in forum C Programming
    Replies: 2
    Last Post: 09-23-2006, 08:53 AM
  4. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 09:16 AM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM

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