小编pir*_*atf的帖子

为什么print语句会改变指针的值?

我写了一个像这样的c ++代码:

#include <iostream>
using namespace std;

int main() {
    int i = 2;
    int i2 = 0;
    void *pi = &i - 1;
    cout << "by cout - the value of *pi is: " << *(int*)pi << endl;
    printf("by printf - the value of *pi is: %d\n", *(int*)pi);
    printf("the address of pi is: %p\n", pi);
    printf("the address of i2 is: %p\n", (void*)&i2);
    printf("the value of i2 is: %d\n", i2);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

by cout - the value of …
Run Code Online (Sandbox Code Playgroud)

c++ pointers c++11

4
推荐指数
1
解决办法
702
查看次数

标签 统计

c++ ×1

c++11 ×1

pointers ×1