Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   pointers (http://www.go4expert.com/forums/pointers-t19318/)

emdivya03 6Sep2009 18:40

pointers
 
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?

Amar_Raj 10Sep2009 18:12

Re: pointers
 
Always remember the Precedence and the associativity of the operators: Refer the link
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
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


All times are GMT +5.5. The time now is 06:00.