T
tfelb
Hi group!
I have here five different declarations but I have some problems to
understand this concept. I know there are more examples if
I would use parentheses but I think the following ones are common.
What I've learned..
int myArray[3] = { 0, 1, 2 };
int *ptr = myArray;
1. *ptr++
2. *++ptr
3. ++*ptr
4. ++*++ptr
5. ++*ptr++
1. printf("%d",*ptr++);
result: 0 first array element
because the postfix increment operator has a higher precedence
than the dereferencing operator. Associativity LEFT to RIGHT. That
would mean the same as *(ptr++) First
incrementing the pointer then the OLDER value will be fetched.
But why the older value? The incrementing process was the first
action?
2. printf("%d",*++ptr);
result: 1 second array element
The dereferencing and the prefix operators have the same
precedence. Associativity RIGHT To LEFT.That
would mean *(++ptr) First incrementing the pointer then fetching
the value.
3. printf("%d",++*ptr)
result: 1 second array element
The dereferencing and the prefix operators have the same
precedence. Associativity RIGHT to LEFT. The
would mean++(*ptr). First fetching the value THEN incrementing.
But why was the result 1 and not 0?
4. printf("%d",++*++ptr)
result: 2 the third array element
The dereferencing and the prefix operators have the same
precedence. Associativity RIGHT to LEFT. That would mean first
incrementing the pointer THEN fetching the value THEN incrementing
that value?
5. printf("%d",++*ptr++);
result: 1 the second array element
The postfix operator has a higher precedence than the prefix and
dereferencing operator. That would mean
the same as ++*(ptr++). First the pointer will be increment then
fetching the value and then incrementing that
value. Here the Associativity is tricky.
I hope I'll get some clarity.
Thanks for any help!
Tom
I have here five different declarations but I have some problems to
understand this concept. I know there are more examples if
I would use parentheses but I think the following ones are common.
What I've learned..
int myArray[3] = { 0, 1, 2 };
int *ptr = myArray;
1. *ptr++
2. *++ptr
3. ++*ptr
4. ++*++ptr
5. ++*ptr++
1. printf("%d",*ptr++);
result: 0 first array element
because the postfix increment operator has a higher precedence
than the dereferencing operator. Associativity LEFT to RIGHT. That
would mean the same as *(ptr++) First
incrementing the pointer then the OLDER value will be fetched.
But why the older value? The incrementing process was the first
action?
2. printf("%d",*++ptr);
result: 1 second array element
The dereferencing and the prefix operators have the same
precedence. Associativity RIGHT To LEFT.That
would mean *(++ptr) First incrementing the pointer then fetching
the value.
3. printf("%d",++*ptr)
result: 1 second array element
The dereferencing and the prefix operators have the same
precedence. Associativity RIGHT to LEFT. The
would mean++(*ptr). First fetching the value THEN incrementing.
But why was the result 1 and not 0?
4. printf("%d",++*++ptr)
result: 2 the third array element
The dereferencing and the prefix operators have the same
precedence. Associativity RIGHT to LEFT. That would mean first
incrementing the pointer THEN fetching the value THEN incrementing
that value?
5. printf("%d",++*ptr++);
result: 1 the second array element
The postfix operator has a higher precedence than the prefix and
dereferencing operator. That would mean
the same as ++*(ptr++). First the pointer will be increment then
fetching the value and then incrementing that
value. Here the Associativity is tricky.
I hope I'll get some clarity.
Thanks for any help!
Tom