By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Multidimensiona Arrays and Pointers

P: n/a
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]; /* ?? */
--
http://arnuld.blogspot.com

Aug 14 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi!

arnuld schrieb:
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
Aug 14 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.