[英]Can we call (void *)0 a void pointer in C?
Summarizing the C standard, specifically ISO/IEC 9899:201x §6.3.2.3 - 3 :总结 C 标准,特别是ISO/IEC 9899:201x §6.3.2.3 - 3 :
If a pointer is being compared to the constant literal 0
, then this is a check to see if the pointer is a null pointer .如果将指针与常量文字
0
进行比较,则这是检查指针是否为空指针的检查。 This 0
is then referred to as a null pointer constant .这个
0
然后被称为空指针常量。 The C standard defines that 0
cast to the type void *
is both a null pointer and a null pointer constant. C 标准定义将
0
强制转换为void *
类型既是空指针又是空指针常量。
Now if we look at (void *)0
it is an address(illegal in this context) that is pointing to void
.现在,如果我们查看
(void *)0
它是一个指向void
的地址(在此上下文中是非法的)。
Normally we can cast such an address to any appropriate pointer datatype and dereference it but here even after casting it to some other pointer type it is illegal to dereference it.通常我们可以将这样的地址转换为任何适当的指针数据类型并取消引用它,但即使在将其转换为其他指针类型之后,取消引用它也是非法的。
So my doubt is:所以我的疑问是:
Can we call (void *)0
as a void pointer looking the way it is defined?我们可以将
(void *)0
称为 void 指针,看看它的定义方式吗?
Also see the below code:另请参阅以下代码:
void *pointer = NULL;
What will I call it now?我现在叫它什么? A void pointer, a null pointer or a null void pointer?
空指针、空指针还是空指针?
What will i call it now ?
我现在叫它什么? A void pointer, a null pointer or a null void pointer ?
空指针、空指针或空空指针?
In this declaration在本声明中
void *pointer = NULL;
there is declared a pointer of the type void *
that is a null pointer
due to initializing it with a null pointer constant.声明了一个
void *
类型的null pointer
由于使用空指针常量对其进行初始化,因此该指针是空指针。
A pointer to object of any type can be a null pointer.指向任何类型对象的指针都可以是空指针。
The casting of the zero integer constant to void *
is used because a pointer of the type void *
can be implicitly converted to any other object pointer type.使用将零整数常量转换为
void *
是因为void *
类型的指针可以隐式转换为任何其他对象指针类型。 Another advantage of using the type void *
is that you may not dereference a pointer of that type because the type void
is always an incomplete type.使用类型
void *
另一个优点是您不能取消引用该类型的指针,因为类型void
始终是不完整的类型。
Early versions of C did not have the type void
. C 的早期版本没有
void
类型。 Instead of the type void
there was used the type char
.使用了
char
类型而不是void
类型。 So for example in old C programs you can encounter something like the following因此,例如在旧的 C 程序中,您可能会遇到以下内容
memcpy( ( char * )p1, ( char * )p2, n );
So my doubt is can we call (void *)0 as a void pointer looking the way it is defined ?
所以我的疑问是我们可以将 (void *)0 称为空指针,看看它的定义方式吗?
It is certainly a void pointer.它当然是一个空指针。 The declaration
void *pointer
means that you declare pointer
as a pointer to void.声明
void *pointer
表示您声明pointer
的指针无效。 pointer
will never be anything else than a void pointer. pointer
永远不会是空指针。 It's type will never change, but the value can change from null to something else.它的类型永远不会改变,但值可以从 null 变为其他值。 Also, the expression
(void *)x
basically mean "cast x
to a void pointer`. So this expression also has the type pointer to void.此外,表达式
(void *)x
基本上意味着“将x
转换为 void 指针”。因此该表达式也具有指向 void 的类型指针。
What will i call it now ?
我现在叫它什么? A void pointer, a null pointer or a null void pointer?
空指针、空指针还是空指针?
I would never use the phrase "null void pointer".我永远不会使用短语“空指针”。 A null pointer is a pointer of any type, pointing at null.
空指针是任何类型的指针,指向空。 A void pointer is simply a pointer pointing to void.
void 指针只是一个指向 void 的指针。 Combining them to one phrase would only call confusion.
将它们组合成一个短语只会让人感到困惑。 It is both a void pointer and a null pointer.
它既是空指针又是空指针。 Any pointer can be a null pointer.
任何指针都可以是空指针。 Consider this:
考虑一下:
int *p = (void*)0;
p
is a null pointer, but it's not a void pointer. p
是空指针,但它不是空指针。
So my doubt is can we call
(void*)0
as a void pointer looking the way it is defined?所以我的疑问是我们可以将
(void*)0
称为 void 指针,看看它的定义方式吗?
(void*)0
is a null pointer constant not a void
pointer. (void*)0
是空指针常量而不是void
指针。
The casted assignment does not define the type of the variable.强制赋值不定义变量的类型。 For example in
int *ptr = (void*)0;
例如在
int *ptr = (void*)0;
, ptr
is an int
pointer, ie a pointer to int
, regardless of the assignment, the same way that void *ptr = (void*)0;
,
ptr
为int
指针,即指针int
,而不管分配的,同样的方式, void *ptr = (void*)0;
is a void
pointer, ie a pointer to void
, both these pointers value is 0
which makes them both null pointers.是一个
void
指针,即指向void
的指针,这两个指针的值都是0
,这使得它们都是空指针。
Read Lundin's comment bellow for interesting extra info about (void*)0
.阅读Lundin 的评论,了解有关
(void*)0
有趣额外信息。
void *pointer = NULL;
What will I call it now ?
我现在叫它什么? A void pointer, a null pointer or a null void pointer ?
空指针、空指针或空空指针?
A null pointer
is a pointer whose value is 0. null pointer
是值为 0 的指针。
A void pointer
is a pointer that has void
type. void pointer
是具有void
类型的指针。
a null void pointer
is a pointer of type void
and its value is 0, it's not a conventional expression but rather a description of what the pointer is and its value ( ie the memory address where it's pointing to, if defined).一个
null void pointer
是类型的指针void
和其值为0,它不是一个常规的表达而是指针是什么的描述,它的值(即,其中它指向中,如果所定义的存储器地址)。
So IMO you can call it all three, deppending on what you want to refer to or the description you are asked for.因此,IMO 可以将其全部称为三个,具体取决于您要参考的内容或要求的描述。
A null
pointer can't be dereferenced being void
or whatever other type. null
指针不能被取消引用为void
或任何其他类型。 Its value is 0, it doesn't point to any valid memory location and that is the only reason why you can't dereference it.它的值为 0,它不指向任何有效的内存位置,这是您不能取消引用它的唯一原因。
For completion, these are all valid statements and will produce the same outcome:为了完成,这些都是有效的语句,将产生相同的结果:
void *pointer = (void*)0;
void *pointer = 0;
void *pointer = NULL;
NULL
can be defined as (void*)0
(null pointer constant), or just 0
(constant literal), both are valid . NULL
可以定义为(void*)0
(空指针常量),或者只是0
(常量文字),两者都是有效的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.