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?
|
Go4Expert Member
|
|
| 10Sep2009,18:12 | #2 |
|
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 |
