<分区>
我了解到所有全局变量都将被初始化为“0”。按照这个,如果我们在全局范围内声明以下行,
static char *pointer;
指针应该等于 NULL。但这总是真的吗?因为在我现在的项目中,我是这样初始化一个指针的。但是当我比较 pointer == NULL 时,它变成了 false 并且它已经被赋值了。是垃圾地址吗?
标签 c
<分区>
我了解到所有全局变量都将被初始化为“0”。按照这个,如果我们在全局范围内声明以下行,
static char *pointer;
指针应该等于 NULL。但这总是真的吗?因为在我现在的项目中,我是这样初始化一个指针的。但是当我比较 pointer == NULL 时,它变成了 false 并且它已经被赋值了。是垃圾地址吗?
最佳答案
所有具有static
存储持续时间(全局或非全局)的对象都将隐式初始化为0
或NULL
如果没有给出显式初始化程序。
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/