这是我写的一个简单的代码。指针p的值是我们知道的数组a的地址。
但是,为什么指针s中没有存放c1的地址呢?
它是如何工作的!
int main(int argc, const char * argv[])
{
int a[4] = {4,3,2,1};
int*p = a;
cout<<&a<<endl;//output 0x7fff5fbff8a0
cout<<p<<endl; //oupput 0x7fff5fbff8a0
char c1[4] = "abc";
char *s = c1;
cout<<&c1<<endl;//output 0x7fff5fbff894
cout<<s<<endl; //output abc
return 0;
}
最佳答案
why does the pointer s not store the address of c1
确实如此。
您看到的是 std::ostream::operator<<
char*
过载,将其视为字符串而不是指针。如果你使用
printf("%p\n", s);
您会看到它按预期工作。
关于c++ - 指向 char 数组的指针,指针的值不是地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19809208/