0

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.

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! That makes perfect sense... On the tutorial I watched - he was entering two five letter names and typed it like this: char myName [5][5] = etc... Threw me off.
While that answer is correct, you could expand it a bit to show what the poster really wants. The poster seems to be under the impression that myName[6][4] is declaring a two element array with the first element being a 6 element array and the second a 4 element array, which isn't correct.
0
char[0] [0][1] [0][2]
char[1] [1][1] [1][2]

And also, you can put an array into an array. So if you imagine this, you can have pages of arrays. Just link do something like..

int a[0] = b[4];

That will put b[4][...] into the single cell of a[0]. Useful for games or whatnot.

2 Comments

So with both arrays and characters you begin counting at 0? (0, 1, 2, etc.) That is to say, char [2] [4] is actually 3 arrays of 5 characters?
And btw, the access to that b[4] is a[0[0]], i believe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.