Search code examples
c++arraysxcodecharosx-yosemite

Excess elements in array initializer C++ char Xcode


My first name has six letters and my second name has four.

The incorrect code will build and print:

char myName[4][6] = {{'x', 'x', 'x', 'x', 'x', 'x'}, {'x', 'x', 'x', 'x'}};

But the correct code will not:

char myName [6] [4] = {{'x', 'x', 'x', 'x', 'x', 'x'}, {'x', 'x', 'x', 'x'}};

Is there a reason that:

  • the second array [4] is being applied to the first set of characters?
  • the first array [6] is being applied to the second set of characters?

I am using the most recent version of Xcode. Is there some sort of glitch where arrays are inverted or something? I've looked around but I can't find anything explaining this.


Solution

  • This is because a char myName[4][6] is a four element array of six char arrays (so your data will fit). where as char myName[6][4] is six arrays of four char arrays (which will not fit your six char array).