pointer to array v/s array of pointers

Discussion in 'C Programming' started by mann!, Feb 25, 2005.

  1. mann!

    mann! Guest

    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
    mann!, Feb 25, 2005
    #1
    1. Advertising

  2. mann!

    Ben Pfaff Guest

    "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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \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\
    );}return 0;}
    Ben Pfaff, Feb 25, 2005
    #2
    1. Advertising

  3. mann!

    mann! Guest

    ( ) 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???
    mann!, Feb 25, 2005
    #3
  4. mann!

    Ben Pfaff Guest

    "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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \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\
    );}return 0;}
    Ben Pfaff, Feb 25, 2005
    #4
  5. mann!

    C_prog Guest

    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_prog, Feb 25, 2005
    #5
  6. mann!

    CBFalconer Guest

    "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
    CBFalconer, Feb 26, 2005
    #6
  7. 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.
    E. Robert Tisdale, Feb 26, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    669
    Chris Torek
    Feb 4, 2005
  2. Sean
    Replies:
    2
    Views:
    629
    loufoque
    Sep 24, 2006
  3. Piotrek

    pointers and array of pointers

    Piotrek, Apr 2, 2007, in forum: C Programming
    Replies:
    8
    Views:
    321
    Chris Torek
    Apr 6, 2007
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    651
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    439
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page