problem to undestand nested pointers

Discussion in 'C Programming' started by happy, Jan 14, 2010.

  1. happy

    happy Guest

    Hello all, Below is a code snippet in C

    #include<stdio.h>
    int main (void)
    {
    static char *s[ ] = {“black”, “white”, “yellow”, “violet”};
    char **ptr[ ] = {s+3, s+2, s+1, s}, ***p;
    p = ptr;
    **++p;
    printf(“%s\n”,*--*++p + 3);
    return 0;
    }

    I am getting some problem in understanding output of this problem.
    Here initially, p contains
    address of first element of ptr i.e. &(s+3), then due to ++p, p is &(s
    +2).
    Now I can't understand how the term *--*++p in printf is evaluated.

    Please Help me understand.
    Is there any good way to understand these type of nested pointers?
     
    happy, Jan 14, 2010
    #1
    1. Advertising

  2. happy

    Tom St Denis Guest

    On Jan 14, 8:12 am, happy <> wrote:
    > Hello all, Below is a code snippet in C
    >
    > #include<stdio.h>
    > int main (void)
    > {
    > static char *s[ ]  = {“black”, “white”, “yellow”, “violet”};
    > char **ptr[ ] = {s+3, s+2, s+1, s}, ***p;
    > p = ptr;
    > **++p;
    > printf(“%s\n”,*--*++p + 3);
    > return 0;
    >
    > }
    >
    >  I am getting some problem in understanding output of this problem.
    > Here  initially, p contains
    > address of first element of ptr i.e. &(s+3), then due to ++p, p is &(s
    > +2).
    > Now I can't understand how the term *--*++p in printf is evaluated.
    >
    > Please Help me understand.
    > Is there any good way to understand these type of nested pointers?


    Don't write code like that? But that being said use your order of
    precedence and left-right associativity to work it out. Break it
    down...

    ++p; // pre increment p
    *++p; // pre increment p, then derefence
    --*++p; // pre increment p, then derefence, then pre-decrement *++p
    *--*++p; // ditto then dereference

    You could write *--*++p ignoring the effects as

    p[1][-1]

    where p[1] points to s+1, (s+1)[-1] points to "black", and "black" + 3
    resolves to "ck\0"

    And indeed the output is "ck\n" as the printf does.

    btw the ** on the **++p; statement doesn't do anything [useful].
    Basically you're preincrementing p then dereferencing it two orders.
    e.g. **++p == "white"

    Tom
     
    Tom St Denis, Jan 14, 2010
    #2
    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. Phil
    Replies:
    1
    Views:
    660
    llewelly
    Sep 16, 2003
  2. muser
    Replies:
    3
    Views:
    775
    Ron Natalie
    Sep 18, 2003
  3. KLEIN Stephane
    Replies:
    0
    Views:
    338
    KLEIN Stephane
    Sep 24, 2007
  4. Replies:
    3
    Views:
    480
    Victor Bazarov
    Dec 7, 2007
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    694
Loading...

Share This Page