pointers

Discussion in 'C' started by emdivya03, Sep 6, 2009.

  1. emdivya03

    emdivya03 New Member

    Joined:
    Sep 3, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    1: main( )
    2: {
    3:
    4: static int a[ ] = {0,1,2,3,4};
    5: int *p[ ] = {a,a+1,a+2,a+3,a+4};
    6: int **ptr = p;
    7:
    8: ptr++;
    9: printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr);
    10:
    11: *ptr++;
    12: printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr);
    13:
    14: *++ptr;
    15: printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr);
    16:
    17: ++*ptr;
    18: printf(“\n %d %d %d”, ptr-p, *ptr-a, **ptr);
    19: }


    plz explain how the *ptr++, *++ptr,++*ptr works?
     
  2. Amar_Raj

    Amar_Raj New Member

    Joined:
    Jun 17, 2009
    Messages:
    34
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Dev
    Location:
    Namma Bengaluru
    Always remember the Precedence and the associativity of the operators: Refer the link
    http://en.wikipedia.org/wiki/Operators_in_C_and_C++
    1. Increment/Decrement(both Prefix and Postfix) has higher precedence than deference(*).
    2. Deference and Prefix Increment/Decrement has same precedence but the associativity is from Right to left
    *ptr++: can be treated as *(ptr++)
    *++ptr: can be treated as *(++ptr) as associativity is Right to left
    ++*ptr: can be treated as ++(*ptr) as associativity is Right to left

    Sharanu,
    Amar
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice