指向字符数组的指针,指针的值不是地址
Pointer to array of char ,the value of the pointer is not an address?
这是我写的一个简单的代码。指针 p 的值是我们所知道的数组 a 的地址。
但是,为什么指针不存储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;
}
为什么指针 s 不存储 C1 的地址
确实如此。
你所看到的是std::ostream::operator<<
对char*
有一个重载,将其视为字符串而不是指针。如果您使用
printf("%pn", s);
您将看到它按预期工作。
它称为运算符重载:
//char* goes here:
std::ostream& operator<<(std::ostream &s, const char* p)
{
//print the string
}
//int* goes here:
std::ostream& operator<<(std::ostream &s, const int* p)
{
//print the address
}
如果将指针投射到 int,您将看到地址。
相关文章:
- CUDA:统一内存和指针地址的更改
- 是否可以仅通过将分配的指针地址存储在C++中来分析内存?
- 向指针地址添加 20 个字节偏移量
- 如何理解智能指针的底层指针地址?
- 初学者问题:C++指针/地址 - 和变量之后不是以前?
- 返回数组中值的指针地址
- C++向指针地址添加 4 个字节
- 指针地址的内存偶尔更改了函数前和后返回
- 如何根据指针地址获取指针内容
- C++ 将指针地址传递给函数
- 直接从指针/地址访问数组的元素
- 如何在没有作弊引擎的情况下从其他应用程序获取指针地址?
- 如何保存指针地址,以便另一个指针可以继续处理它
- 间接寻址运算符如何返回带有运算符重载的指针地址
- 使用已删除的指针地址.据我所知,
- 如何保护类外用户可以修改的指针地址?C++
- 多个调用构造函数将更改C 中的成员指针地址
- 类对象指针的向量:如何在指针地址获得价值?C 和SDL_RECT
- Visual Studio C 2K15-在指针地址上获取错误
- 指针地址和参考混乱