我想访问和修改一个在全局作用域定义的函数中的静态变量,但是输出的第二行和第三行是意外的,为什么ox是0?
#include <iostream>
using namespace std;
int * foo(){
static int *x = new int(5);
cout << *x << '\t' << &x << endl;
*x++;
return x;
}
int main(){
int * ox;
ox = foo();
cout << *ox << '\t' << &ox << endl;
*ox++;
cout << *ox << '\t' << &ox << endl;
int * t= foo();
}
输出结果为(在我的机器上):
5 0x6021d0
0 0x7fffdc82d3a0
0 0x7fffdc82d3a0
0 0x6021d0
欢迎提出任何意见。