Multidimensiona Arrays and Pointers

A

arnuld

this is from section 7.2.4 of C++ Primer 4/e

int *matrix[10]; /* an array of 10 pointers */

int (*matrix)[10]; /* poiter to an array of 10 ints */

if that is true then, is this right:

int* matrix[10]; /* an array of 10 pointers to ints */

can't i write a replcement of author's 2nd example above ?

and what about these:

(int*) matrix[10]; /* ?? */

int (*) matrix[10]; /* ?? */
 
F

Frank Birbacher

Hi!
if that is true then, is this right:

int* matrix[10]; /* an array of 10 pointers to ints */

Yes, correct. You should learn a bit about parsing tokens. Your array
definition is parsed into distinct "parts" (tokens):
"int" "*" "matrix" "[" "10" "]" ";"

and the compiler drops the white space (spaces, tabs, newlines). So the
following are all identical:

int*matrix[10];
int * matrix [ 10 ] ;
int* matrix
[
10
];

So don't be afraid of "int* matrix" and "int *matrix" doing something
different.
can't i write a replcement of author's 2nd example above ?

I can't think of one.
and what about these:

(int*) matrix[10]; /* ?? */

I took me a while to recognize it! This is valid syntax but it is not a
variable declaration. Instead it reads index "10" from the variable
"marix" (which needs to be declared before) and casts the result into
"int*" whereupon this result is discarded/dropped/ignored.
int (*) matrix[10]; /* ?? */

This is invalid syntax. So it has no meaning.

Frank
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top