user2293708
user2293708

Reputation: 19

precedence of ++ (post,prefix) nd dereference operator

Shouldn't the output of the following code be f

I get an output e

#include<stdio.h>

void main(){

    char arr[]="Geeks";
    char *ptr = arr;
    ++*ptr++;
    printf("%c\n",*ptr);

}

Upvotes: 1

Views: 238

Answers (2)

siddstuff
siddstuff

Reputation: 1255

Yes expression is parsed as ++*((ptr++)), first ptr++ is calculated but because it is postfix increment the new calculated value doesn't update the old value of ptr until the statement ends (;) . Next ++**( ptr++ ) is calculated on old value of ptr that result , G change to H. Now all work is done, the statement ends and ptr value is updated, that points to next element that is e.

Upvotes: 2

user529758
user529758

Reputation:

No, it shouldn't. Your code increments the first character and then moves the pointer one forward. The pointer will point to the first e, and depending on your locale/character encoding, the first letter is most probably H. The expression is parsed according to precedence and associativity rules as:

++(*(p++))

Upvotes: 4

Related Questions