Difference between array of pointers and a 2-D array

2255 views arrays
1

int *m[10], n[10][10];

Which of the following would give error and why?

1) m[2][4]
2) n[2]
3) m[3]

(PS: Please explain in detail)

answered question

You need to add more information; are you trying to print out values from those indices, set something to them, or what? Are you creating any other memory that any of the pointers in m point to? There just isn't enough information given.

1 Answer

11

ANSWER:

1) would throw an error. m is a one-dimensional array of int pointers, so there isn't a second dimension to m.

EXPLANATION:

1) Like I said above, m is a one-dimensional array of int pointers. Trying to access a second dimension would throw an error and prevent the compiler from compiling the program.

2) Asking for n[2] means "get the third row of n". n is a two-dimensional array of ints, so this is still valid. n[2] will return the array contained at the third index of n.

3) Asking for m[3] means "get the fourth item in m". This is also valid.

posted this

Have an answer?

JD

Please login first before posting an answer.