Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer to array v/s array of pointers

Reply
Thread Tools

pointer to array v/s array of pointers

 
 
mann!
Guest
Posts: n/a
 
      02-25-2005
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

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      02-25-2005
"mann!" <> writes:

> 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 [].
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
 
Reply With Quote
 
 
 
 
mann!
Guest
Posts: n/a
 
      02-25-2005
( ) 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???

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      02-25-2005
"mann!" <> writes:

> ( ) 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.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
 
Reply With Quote
 
C_prog
Guest
Posts: n/a
 
      02-25-2005
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



 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-26-2005
"mann!" wrote:
>
> ( ) 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.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      02-26-2005
Manan wrote:

> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Pointer to array of array of const pointer RSL C++ 14 02-19-2010 02:06 PM
Array of pointer and pointer of array erfan C Programming 6 01-28-2008 08:55 PM
Array of pointer Vs Pointer to Array sangeetha C Programming 9 10-09-2004 07:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57