【发布时间】:2020-10-05 07:17:20
【问题描述】:
我知道保存数组的变量存储数组的地址,并且对它的任何索引都会对其执行指针运算。在我复习基础知识时,我发现了这一点。
int main() {
int equal;
char c[] = {'a', 'b', 'c'};
char* p = c;
char* q = &c[0];
cout<<*p<<endl;
cout<<*q<<endl;
cout<<&p<<endl;
cout<<&q<<endl;
if (c==q) {
equal=1;
}
cout<<equal<<endl;
}
//output
a
a
0x7ffda22e0a60
0x7ffda22e0a68
1
取消引用 p,q 给了我相同的值,但 p,q 的地址不同。这怎么可能?
【问题讨论】:
-
这相当于:我怎样才能在两张纸上写上我的家庭住址,同时让两张纸上的家庭住址保持不变?
标签: c++ arrays pointers pointer-arithmetic