字符数组如何存储在内存中?
How is char array stored in memory?
我对字符数组的内存地址感到困惑。以下是演示代码:
char input[100] = "12230 201 50";
const char *s = input;
//what is the difference between s and input?
cout<<"s = "<<s<<endl; //output:12230 201 50
cout<<"*s = "<<*s<<endl; //output: 1
//here I intended to obtain the address for the first element
cout<<"&input[0] = "<<&(input[0])<<endl; //output:12230 201 50
字符数组本身是一个指针吗?为什么&运算符不给出char元素的内存地址?如何获取单个条目的地址?谢谢!
在最后一行中,表达式确实&(input[0])
产生 char 数组第一个 char 的地址,即 char 数组input
的地址。所以你的代码有点工作。
但是输出运算符<<
有一个有用的重载char *
并将 char 数组的竞争打印为 C 字符串(打印所有字符,直到找到零字符)。
要打印地址,请执行以下操作:
void *p = input;
std::cout << "p=" << p << "n";
尽管在大多数情况下,数组可以被认为是指向数组中第一个元素的指针,但它们并不等同于指针。从技术上讲 - 数组衰减到指针。
&(input[0])
返回一个char*
,该具有打印实际字符串的重载。要打印地址,您可以使用static_cast<void*>(&input[0])
.
相关文章:
- 将字符串存储在c++中的稳定内存中
- 类型总是使用其大小存储在内存中吗
- 使用无符号字符数组有效存储内存
- 具有内存顺序的原子负载存储
- CertGetCertificateChain 具有支持的内存存储和证书信任列表
- 为什么 bool 和 _Bool 如果它们在内存中占用 1 个字节,它们只能存储 0 或 1
- 具有相同特征的两个对象是否只在内存中存储一次?无论定义它们的函数是什么,都是不同的
- 存储在哪个内存段(代码/数据段)类(员工)中?
- 可用存储空间会消耗更多剩余内存吗?
- 是否可以仅通过将分配的指针地址存储在C++中来分析内存?
- 将数据存储在内存中以供以后访问
- 英特尔将指令存储在重叠的内存区域
- 关于矢量内存存储
- 如何使一个线程中的内存存储"promptly"在其他线程中可见?
- c++中用于常量数据的内存存储
- 内存存储可以在 OoOE 处理器中真正重新排序吗?
- 动态分配的内存存储说明
- 在 std::vector 中存储指针会破坏连续内存存储的优势
- #定义指令内存存储
- 跨语言的内存存储