declaring 2-D array of Char

Hi everyone
I tried to declare a 2-D array like this

char array2[100][16] = "";

And it did not compile

so i declared it like this instead

char array2[100][16] = {};

What is the difference between these two? I mean, would it work the same as if it was a 1-D array [100] = " ";

What i am going to be doing with the 2-D array is putting words in it.
char array2[100][16] = "";


C++ allows you to construct char arrays with a string literal.

However that is not what you're doing here. 2D arrays are actually arrays of arrays. So 'array2' is not a char array, it's an array of char arrays. And therefore it doesn't make any sense to assign a string literal to it.

Confusing? Yes, MD arrays are confusing, which is why I avoid them like the plague even though I understand them.

char array2[100][16] = {};


This works because arrays initialized with the empty braces {} have all of their elements default constructed. array2 is an array (not of chars, but it's still an array), so this is legal.

Each of array2's arrays get default constructed, which in turn default constructs each of those arrays (resulting in all chars being zero'd).

What i am going to be doing with the 2-D array is putting words in it.


strings strings strings. Get away from that char array crap.

1
2
3
4
5
string words[100];

// or

vector<string> words;
Last edited on
great help, thank you
Topic archived. No new replies allowed.