简体   繁体   中英

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);

}

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++))

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM