c - c在第一次定义时如何给指针数组赋值?

标签 c

我有一个 int 指针数组。

int *ints[3];

定义这个数组后内存会发生什么变化?存储的初始指针值是多少?当我尝试取消引用一个值时,我得到一个 segmentation fault,它告诉我这些值是任意的。

最佳答案

这取决于数组的存储。

  • 如果它有静态存储,即在任何函数之外声明或 在函数中用 static 声明,则所有元素都将为 NULL

  • 如果在函数中声明,没有static,它将有 自动存储且初始值不确定

标准恰好在这件事上非常清楚并且很有帮助:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then:

  • if it has pointer type, it is initialized to a null pointer;
  • if it has arithmetic type, it is initialized to (positive or unsigned) zero;
  • if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero
    bits;

关于c - c在第一次定义时如何给指针数组赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21840919/

相关文章:

c - 如何评估语句*ptr1++ = *ptr2++?

c - 避免 CUDA 字符串搜索中的分支发散

c - C解释中的运算符优先级

c - 从字符串中删除指定字符的有效方法

c - 从不兼容的指针类型传递参数 1 of 'recievematrix'

c++ - 在 C/C++ 中读取 OpenOffice 文件

c - 与用户创建的结构不兼容的类型

c - 在循环内交替执行两条语句

c - fork() 的异步信号安全

c - 帮助 C 中的结构和循环