什么类型是NULL? [英] What type is NULL?

查看:58
本文介绍了什么类型是NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 C 中的 Null 类型是什么.这可能是重复的,但我一直在搜索有关 void 类型的信息.也许更好的方法是可以为任何类型函数返回 NULL 吗?例如:

I'm wondering what type Null is in C. This is probably a duplicate, but I kept getting information about void type on searches. Maybe a better way is can NULL be returned for any type function? For example:

int main(){
    dosomething();
    return NULL;
}

那行得通吗?

推荐答案

NULL 的类型可以是整数类型或 void *.这是因为 C 标准允许将其定义为整数常量表达式或转换为 void * 的结果.

The type of NULL may be either an integer type or void *. This is because the C standard allows it to be defined as either an integer constant expression or the result of a cast to void *.

C 2018 7.19 3 说 NULL扩展为实现定义的空指针常量"(当包含多个标头中的任何一个时:).

C 2018 7.19 3 says NULL "expands to an implementation-defined null pointer constant" (when any of several headers have been included: <locale.h>, <stddef.h>, <stdio.h>, <stdlib.h>, <string.h>, <time.h>, or <wchar.h>).

C 6.3.2.3 3 说空指针常量是值为 0 的整数常量表达式,或者这样的表达式转换为 **void *** 类型."

C 6.3.2.3 3 says a null pointer constant is "An integer constant expression with the value 0, or such an expression cast to a type **void ***."

因此,C 实现可以将 NULL 定义为,例如:

Thus, a C implementation may define NULL as, for example:

  • 0,类型为int
  • ((void *) 0),类型为void *,或
  • (1+5-6),这是一个整数常量表达式,值为0,类型int.
  • 0, which has type int,
  • ((void *) 0), which has type void *, or
  • (1+5-6), which is an integer constant expression with value 0 and type int.

即使 NULL 可能具有整数类型,它也可以与指针进行比较和分配,如 if (p == NULL) ….这些操作的规则规定,整数常量零将被转换为适合该操作的指针类型.

Even though NULL may have an integer type, it may be compared to and assigned to pointers, as in if (p == NULL) …. The rules for these operations say that an integer constant zero will be converted to the appropriate pointer type for the operation.

尽管 NULL 可以定义为 0,但它旨在用于指针,而不是作为整数零.程序应该避免这样做,而 C 实现通常最好将其定义为 ((void *) 0) 以帮助避免可能被接受为整数值的错误.

Although NULL may be defined to be 0, it is intended to be used for pointers, not as an integer zero. Programs should avoid doing that, and C implementations are generally better off defining it as ((void *) 0) to help avoid mistakes where it might be accepted as an integer value.

在大多数 C 实现中,将 NULL 转换为整数将产生零.但是,这在 C 标准中并不能保证.允许 (int) NULL(uintptr_t) NULL 将产生一些特殊的不使用"位置的地址而不是零.甚至 (int) (void *) 0 也可能产生这样的地址而不是零.

In most C implementations, converting NULL to an integer will yield zero. However, this is not guaranteed in the C standard. It is allowed that (int) NULL or (uintptr_t) NULL will produce the address of some special "do not use" location rather than zero. Even (int) (void *) 0 might produce such an address rather than zero.

当整型常量转换为指针类型时,C实现会对其进行特殊处理;即使它的空指针使用非零地址,它也会为该实现生成一个空指针.它是一个整数常量这一事实意味着编译器可以在它识别源代码中的转换时应用这种特殊处理.如果我们有一些非常量表达式,比如一个 int 变量 x,那么 (void *) x 不能保证产生一个 null指针,即使 x 的值为零.

When an integer constant is converted to a pointer type, it is treated specially by the C implementation; it produces a null pointer for that implementation even if its null pointer uses an address other than zero. The fact that it is an integer constant means the compiler can apply this special treatment where it recognizes the conversion in the source code. If we have some non-constant expression, such as an int variable x, then (void *) x is not guaranteed to yield a null pointer even if the value of x is zero.

这篇关于什么类型是NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆