c - 在 C 中,静态全局指针是否总是初始化为 NULL?

标签 c

<分区>

我了解到所有全局变量都将被初始化为“0”。按照这个,如果我们在全局范围内声明以下行,

static char *pointer;

指针应该等于 NULL。但这总是真的吗?因为在我现在的项目中,我是这样初始化一个指针的。但是当我比较 pointer == NULL 时,它变成了 false 并且它已经被赋值了。是垃圾地址吗?

最佳答案

所有具有static 存储持续时间(全局或非全局)的对象都将隐式初始化为0NULL 如果没有给出显式初始化程序。

Chapter and verse :

6.7.9 Initialization
...
10 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;
— if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

关于c - 在 C 中,静态全局指针是否总是初始化为 NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30925425/

相关文章:

使用 C 将文件从一个目录复制到另一个目录。在发送目录中打开文件时出现问题。没有这样的文件或目录

c++ - 从 Eclipse IDE 运行 C 程序时如何为其指定命令行参数

c - Windows 控制台下的 Stdin 和 EOF 行为

c - 有没有一种简单的方法可以从文件中使用 SDL 创建字符串?

c - 让子进程等待另一个for循环

c - 关于从寄存器GPIODATA读取引脚

c - 如何获取此字符串的信息?

c++ - GDB 显示堆栈帧的函数参数不正确

c - 将部分字符串匹配到字符串数组中的完整字符串

c - Socket - 当有2个连续的crlf时接收客户端请求