简体   繁体   中英

(const char*)++ or (const char)++?

#include <iostream>
#include <cstring>

using namespace std;

int main() {
    char *str = "hello";

    while (*str) {
        cout << *str;
        *str++;
    }

    return 0;
}

and

#include <iostream>
#include <cstring>

using namespace std;

int main() {
    char *str = "hello";

    while (*str) {
        cout << *str;
        str++;
    }

    return 0;
}

both output

hello

Why doesn't adding or removing the deference operator before str++ alter the output?

Postfix ++ has higher precedence than the de-rerference operator * , so

*x++;

is the same as

*(x++);

which really does the same as

x++;

*str++ means *(str++) .

Since you don't use the value of that expression, the * has no effect.

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