是否可以将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
正在将强制转换为char
的NULL
值分配给内存中的某个随机地址.
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\n");
return EXIT_SUCCESS;
}
实际上可以打印
为NULL
is NULL
当我编译并运行它时,我担心我依赖未定义的行为,或者至少是未指定的行为,我应该写
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 是的,非常.
The actual claim made on the guide reads like
另一方面,如果仅使用单个初始分配
int *my_int_ptr = 2;
,则程序将尝试用值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.
对于该语句,(暂时忽略指向整数转换的指针是实现定义的行为的事实)
For the statement, (ignoring, for now, the fact that pointer to integer conversion is an implementation-defined behaviour)
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.
并且它们不是兼容"类型,因此此初始化无效,因为它违反了伦丁的答案.
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屋!