是否可以将 C 指针初始化为 NULL? [英] Is it possible to initialize a C pointer to NULL?
问题描述
我一直在写类似的东西
char *x=NULL;
假设
char *x=2;
将创建一个指向地址 2 的 char
指针.
would create a char
pointer to address 2.
但是,在 GNU C 编程教程中 它说 int *my_int_ptr = 2;
将整数值 2
存储到分配时 my_int_ptr
中的任何随机地址.
But, in The GNU C Programming Tutorial it says that int *my_int_ptr = 2;
stores the integer value 2
to whatever random address is in my_int_ptr
when it is allocated.
这似乎意味着我自己的 char *x=NULL
正在分配 NULL
转换为 char
的任何值内存中的一些随机地址.
This would seem to imply that my own char *x=NULL
is assigning whatever the value of NULL
cast to a char
is to some random address in memory.
虽然
#include <stdlib.h>
#include <stdio.h>
int main()
{
char *x=NULL;
if (x==NULL)
printf("is NULL
");
return EXIT_SUCCESS;
}
确实会打印
为空
当我编译和运行它时,我担心我依赖于未定义的行为,或者至少是未指定的行为,我应该写
when I compile and run it, I am concerned that I am relying on undefined behavior, or at least under-specified behavior, and that I should write
char *x;
x=NULL;
改为.
推荐答案
是否可以将 C 指针初始化为 NULL?
TL;DR是的,非常.
另一方面,如果你只使用单个初始赋值,int *my_int_ptr = 2;
,程序将尝试填充由 my_int_ptr
的值为 2.由于 my_int_ptr
被垃圾填充,它可以是任何地址.[...]
On the other hand, if you use just the single initial assignment,
int *my_int_ptr = 2;
, the program will try to fill the contents of the memory location pointed to bymy_int_ptr
with the value 2. Sincemy_int_ptr
is filled with garbage, it can be any address. [...]
嗯,他们是错了,你是对的.
Well, they are wrong, you are right.
对于语句,(暂时忽略指向整数转换的指针是实现定义的行为这一事实)
int * my_int_ptr = 2;
my_int_ptr
是一个变量(类型为指向 int
的指针),它有自己的地址(类型:指向整数的指针的地址),您正在存储一个2
的值转换为 那个 地址.
my_int_ptr
is a variable (of type pointer to int
), it has an address of its own (type: address of pointer to integer), you are storing a value of 2
into that address.
现在,my_int_ptr
,作为一个指针类型,我们可以说,它指向type"的值.在 指向 my_int_ptr
中保存的值的内存位置.因此,您实际上是在为指针变量分配 of 的值,而不是指针指向的内存位置的值.
Now, my_int_ptr
, being a pointer type, we can say, it points to the value of "type" at the memory location pointed by the value held in my_int_ptr
. So, you are essentially assigning the value of the pointer variable, not the value of the memory location pointed to by the pointer.
所以,总结一下
char *x=NULL;
将指针变量x
初始化为NULL
,而不是指针指向的内存地址处的值.
initializes the pointer variable x
to NULL
, not the value at the memory address pointed to by the pointer.
这与
char *x;
x = NULL;
扩展:
现在,严格遵守,像这样的陈述
Expansion:
Now, being strictly conforming, a statement like
int * my_int_ptr = 2;
是非法的,因为它涉及违反约束.说清楚,
is illegal, as it involves constraint violation. To be clear,
my_int_ptr
是指针变量,类型int *
- 一个整数常量,
2
根据定义具有int
类型.
my_int_ptr
is a pointer variable, typeint *
- an integer constant,
2
has typeint
, by definition.
它们不是兼容的";类型,所以这个初始化是无效的,因为它违反了简单赋值的规则,在第 6.5.16.1/P1 章中提到,在 Lundin 的回答中描述.
and they are not "compatible" types, so this initialization is invalid because it's violating the rules of simple assignment, mentioned in chapter §6.5.16.1/P1, described in Lundin's answer.
如果有人对初始化如何与简单的赋值约束相关联感兴趣,请引用 C11
,第 6.7.9 章,P11
In case anyone's interested how initialization is linked to simple assignment constraints, quoting C11
, chapter §6.7.9, P11
标量的初始值设定项应该是一个表达式,可选地用大括号括起来.这对象的初始值是表达式的初始值(转换后);同一类型适用于简单赋值的约束和转换,采用标量的类型成为其声明类型的非限定版本.
The initializer for a scalar shall be a single expression, optionally enclosed in braces. The initial value of the object is that of the expression (after conversion); the same type constraints and conversions as for simple assignment apply, taking the type of the scalar to be the unqualified version of its declared type.
这篇关于是否可以将 C 指针初始化为 NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!