Does anyone know how to use a for loop to clear a 2D array? To clear a 1D array, for example you might do this:

char array[10];
for (int ct; ct<=10; ct++)
     array[ct] = "\0";

What I'd like to know is how to clear a 2D array in the same way. I believe I will have to nest two or more for loops. All help is appreciated!! Thanks!! ^_^

Recommended Answers

All 6 Replies

>>array[ct] = "\0";
That is incorrect. The quotes should be single quote, liks this: array[ct] = '\0'; >>What I'd like to know is how to clear a 2D array in the same way
Yes, its done the same way.

char array[10][20];
for(int i = 0; i < 10; i++)
    array[i][0] = '\0';

The above only clears the first byte of each string, making them empty strings. If you want to clear all bytes of each string then you have a couple options:
1) add another loop inside the outer loop -- its called nested loops

2) clear the strings when initilzed, like this: char array[10][20] = {0};

to "clear" the char array if it is used to hold strings you only need to assign the null char to index 0.

Note that '\0', \0, and 0 are all ways of indicating null char but "\0" isn't anything really.

To put a null char in each index zero you just need to loop the array like this:

char 2Darray[10][7];
for (int ct = 0; ct < 10; ct++) /*note that you need to give ct some starting value before it's used in the loop*/
     2Darray[ct][0] = '\0';

You realize of course that the array is never really clear. There will always be information associated with every element. That information may be random (junk), a clear default (like null chars), some stuff left over from a previous use (junk again for most purposes) or the real deal.

Thanks for the help from both of you, and sorry about writing "\0". I kind of wrote it quickly and forgot to use single quotes. Also thanks for informing me that '\0' only clears the first byte, that will definitly come in help and so wil {0}. But in both of the examples you gave for clearing a 2D array the array variable that was being cleared only had a variable in the first brackets and a 0 in the second. Wouldn't this only clear the first "axis", if you will, of the array?

char array[10][20];
for(int i = 0; i < 10; i++)
array[0] = '\0';

Wouldn't this clear only:

What about the other 90?
i.e. array[0][1], array[5][5], array[3][9], etc.

It isn't necessary in most cases to actually clear anything other than the first byte of each array. Why? Because they are normally treated as C-style strings which can be used with all the c functions in string.h (or cstring). The one method I posted using {0} when the array is declared actually does clear every byte of the array.

Thank you for the help Ancient Dragon, but I guess I didn't explain my problem correctly. I've figured out and tested a solution that work for what I need. For the problem I had the for loops would have messed up the buffers I had set up anyways so in stead I used if loops. heres basically what I did to help you understand what I was trying to do. Feel free to comment and help me make my problem more clear next time around. ^_^

int ct = 0;
int ctt = 0;
char array[10][10];

//keep in mind the part after varible decloration is in a repearting loop

if (ct < 10)
     if (ctt < 10)
          array[ct][ctt] = '\0';
          ctt = ctt + 1;
          if (ctt == 10)
               ct = ct + 1;
               ctt = 0;
     if (ct == 10)
          ct = 0;

I'm pretty sure that ever 10 times it goes through the repeating loop that isn't shown that array[][] will be cleared. Please tell me if there is anything wrong with this!! ^_^

My brain won't read stuff outside of code tags right now...

Just set the entire block to zero. You can do it with a nested loop, or just one loop, or just using a library function.

#include <cstring>
  char array[7][10];  // seven strings of 10 chars each

  // using nested loop
  for (int y=0; y <  7; y++)
  for (int x=0; x < 10; x++)
    array[y][x] = '\0';

  // using one loop
  for (int i=0; i < 7 * 10; i++) *(array + i) = '\0';

  // using library function
  memset( array, '\0', sizeof( array ) );

The nested loop is least efficient; the library function is most efficient.

BTW, never name a variable "array"...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.