pointer to array v/s array of pointers

M

mann!

hi

can some one please explain how

int (*x)[10] declares a pointer to an array

and

int *x[10] declares an array of pointers?

....i just cant figure out how to interpret [] in an expression so if
you could put in a few lines about that too..
thanks in anticipation

Manan
 
B

Ben Pfaff

mann! said:
can some one please explain how

int (*x)[10] declares a pointer to an array

and

int *x[10] declares an array of pointers?

[] has higher precedence than *, but () has higher precedence
than [].
 
M

mann!

( ) has higher precedence , so for int (*x)[20] doesnt that mean it
defines an array of (*x) ie pointer to int, because (*x) is interpreted
as pointer first???
 
B

Ben Pfaff

mann! said:
( ) has higher precedence , so for int (*x)[20] doesnt that mean it
defines an array of (*x) ie pointer to int, because (*x) is interpreted
as pointer first???

You appear not to understand the concept of precedence. () has
higher precedence, so "operators" inside it are interpreted
first.
 
C

C_prog

Hi

*x means 'pointer to' and not 'pointer of'
so (*x)[20] id pointer to array of 20 elements.

Are u or anyone else u know of is doing self-study in C-programming in a
time bound schedule?

Thanks
 
C

CBFalconer

mann! said:
( ) has higher precedence , so for int (*x)[20] doesnt that mean it
defines an array of (*x) ie pointer to int, because (*x) is interpreted
as pointer first???

*x defines an int. So does (*x). Thus the appended [] makes the
result an array of ints. Meanwhile "int *x" defines x as a pointer
to int. Think of "int* x" which is the same thing with white space
moved around. The appended [] makes the result an array of
pointers.
 
E

E. Robert Tisdale

Manan said:
Can some one please explain how

int (*x)[10];

declares a pointer to an array and

int *x[10];

declares an array of pointers?

I don't like to write

int *p;

It appears to imply that
you are declaring in object of type int
which you can reference with *p
but, in fact, *no* such object is created.
p is an [uninitialized] pointer to an object of type int.

I prefer to write

int* p;

for a pointer to an int.
*p is a reference to an int through pointer p
so, if I write

int (*p)[10];

*p must be a reference to an array of 10 int though pointer p
but

int* p[10];

is an array of 10 pointers to objects of type int.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top