0

I realize theres a lot of array and pointer questions, but I had one thats extremely specific... Its actually from a test I took in class awhile ago and I'm still having trouble with it.

The question is - Write out a complete declaration for
A variable named pmatrix that is a pointer to an array of 8 arrays of 10 pointers to integers

so far I'm thinking something like
int*pmatrix[8][10] ,

more concerned with a good explanation than just an answer.
thanks!

2
  • 3
    cdecl.org is your friend. Commented Apr 30, 2011 at 23:08
  • @Oli Thanks for the link - very cool! Commented Apr 30, 2011 at 23:11

4 Answers 4

4

A variable named pmatrix that is a pointer:

*pmatrix

to an array of 8

(*pmatrix)[8]

arrays of 10

(*pmatrix)[8][10]

pointers to integers:

int *(*pmatrix)[8][10]

Substituting into cdecl, we are told the following:

declare pmatrix as pointer to array 8 of array 10 of pointer to int

which is where we started!

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

Comments

1
int*pmatrix[8][10]

There's an issue of precedence: [] has a higher precedence than '*', so that is an array[8] of array[10] of pointer to int. You need to add parentheses to override the precedence:

int (*pmatrix)[8][10]

(More parentheses are possible. I'm not sure that

int (((*pmatrix)[8])[10]);

would be an improvement, however:-).)

2 Comments

All correct, except that it needs to be arrays of pointers-to-int, not just int.
Oops. But the basic principle holds: type specification behave more or less like expressions, and you (sometimes) need parentheses to enforce precedence.
0

The answer to your question would be

int* (*pmatrix)[8][10];

Remember though, arrays are just pointers to the first element of the array, so an array is a pointer and a pointer is an array (sometimes of just one element).

Note though, that beneath this array is really just a single-dimensional array of 80 elements. If you do an index like this:

int pmatrix[8][10];
pmatrix[3][5];

The compiler treats that as if you did:

*(pmatrix + (3 * 10) + 5);

because the index [3][5] accesses the 5th element (+ 5) of the 3rd subarray (3 * 10 (10 being the size of each sub array)).

37 Comments

-1: No, that's not the same. A pointer to an array is not the same as an array.
@Oli I never said a pointer to an array is the same as an array, I said the name of an array is just a pointer to the first element of the array. Please remove your downvote.
@Seth: You basically did, when you said that int *pmatrix[8][10] is the right answer. It's not the right answer. The right answer is int *(*pmatrix)[8][10].
@Seth: a (in int a[5]) is no more a pointer than b is in int b;.
@Seth: It's a pointer, both semantically and under-the-hood. Casually, we might say that it's "a pointer to an array", or even just "an array", but technically that's incorrect. The C/C++ standards are clear that an array is something like int a[5], and that the result of new (and malloc) is simply a pointer to "allocated space".
|
0
int** pmatrix = new int*[8]; // Array of 8 pointers to arrays
for(int i = 0; i < 8; ++i) pmatrix[i] = new int[10]; // Create each array

Comments

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.