Declaration syntax

L

LRS Kumar

int* p[10] would mean that p is an array of 10 pointers to int.

I was a little puzzzled when I came across the following:

int (*p)[10];


What exactly does this one mean?


Thank you for your time.
 
L

Leor Zolman

int* p[10] would mean that p is an array of 10 pointers to int.

I was a little puzzzled when I came across the following:

int (*p)[10];


What exactly does this one mean?

p is a pointer to an array of 10 ints.

The vast majority of the times when you wish to point to an array, you do
it by defining a pointer to the element type, and setting that pointer to
point to the first element of the array:

int a[10];
int *pi;
...
pi = a; // same as: pi = &a[0];

Using a bona-fide pointer-to-array comes into use more when having to point
to multi-dimensional arrays, which C/C++ does not support in any especially
friendly way. I could not find one example (and I tried for a while) of
when a pointer to a 1-dimensional array of T's would be preferable to just
using a pointer-to-T, but I've no doubt there must be at least one
somewhere (well, almost no doubt; if no one provides an example here, try
comp.lang.c). In the meantime, for a good tutorial document on declarations
that includes quite a bit more than you'd typically expect to find on
pointer-to-arrays, check this out:
http://www.djmnet.org/lore/c-declarations.txt

HTH,
-leor
 

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
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top