我想知道如何用C语法表征NULL,它会不变?
NULL
is a macro object defined in some standard header <stddef.h>
. On my Debian/Sid/x86-64 system with GCC 4.8 it is defined as follow (lines 394 and following of the compiler specific /usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h
header file)
/* A null pointer constant. */
#if defined (_STDDEF_H) || defined (__need_NULL)
#undef NULL /* in case <stdio.h> has defined it. */
#ifdef __GNUG__
#define NULL __null
#else /* G++ */
#ifndef __cplusplus
#define NULL ((void *)0)
#else /* C++ */
#define NULL 0
#endif /* C++ */
#endif /* G++ */
#endif /* NULL not defined and <stddef.h> or need NULL. */
#undef __need_NULL
BTW, a pointer which has been dereferenced is known to not be the null pointer value. In other words, GCC is permitted to optimize (assuming p
has been declared as int *p;
)
int x= *p;
if (!p) do_very_complex_stuff();
// or: if (p == NULL) do_very_complex_stuff();
as simply
int x= *p;
at least outside of freestanding programs.
IIRC, that optimization made a harsh debate between Linus Torvalds and some GCC developers.
Pedantically the NULL
pointer (ie the null pointer value) is perhaps not required to be all-zero bits, but I don't know any implementation doing that today.
NULL
is not part of C grammar. NULL
is #define
d in stddef.h
standard header.
像这样的宏:
#define NULL ((void*)0)
C11dr §6.3.2.3 3 NULL is a null pointer constant.
"An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant..." Footnote: "The macro NULL is defined in <stddef.h>
(and other headers) as a null pointer constant."
Other pointer values could also be a "null pointer constant". They would all compare equal.
在C中,NULL不是语法的一部分,它只是一个宏。
NULL不是C语法的一部分,这就是为什么你必须包含一个标题来避免编译器警告(它可能取决于你的编译器设置)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.