c / c++ null 0 (void*)

abccba 2003-04-01 09:10:51
在VC中, 空指针被定义成:

#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void*) 0)
#endif
#endif


为什么c 与c++ 的不一样
...全文
177 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
用户 昵称 2003-04-04
  • 打赏
  • 举报
回复
c++更严格吧。
同桌老王 2003-04-03
  • 打赏
  • 举报
回复
其实都可以的,只是要类型转换的时候要出问题。
你在C中类型转换不是那么严格,不能体会到这个变化
maurice1983 2003-04-03
  • 打赏
  • 举报
回复
因为你的空指针 可能 随时要指向一个对象啊,就有可能导致类型的转化啊!
robertnet 2003-04-03
  • 打赏
  • 举报
回复
maurice1983(吸水海绵): 为什么要转化为其他类型呢?
robertnet 2003-04-03
  • 打赏
  • 举报
回复
maurice1983(吸水海绵): 你的话有出处吗?
maurice1983 2003-04-02
  • 打赏
  • 举报
回复
这就是c++中的定义,必须转换成其他的类型,而只有0可以转换成其他类型,void* 0不能转换成其他类型!
就怎么简单!
darkhawk 2003-04-02
  • 打赏
  • 举报
回复
stroustrup在他的the c++ programming language中对这有详细的描述,他建议在c++中使用0而不是null.
用户 昵称 2003-04-02
  • 打赏
  • 举报
回复
study
shdiao 2003-04-01
  • 打赏
  • 举报
回复
学习!
ckacka 2003-04-01
  • 打赏
  • 举报
回复
According to the language definition, a constant 0 in a pointer
context is converted into a null pointer at compile time. That is, in
an initialization, assignment, or comparison when one side is a
variable or expression of pointer type, the compiler can tell that a
constant 0 on the other side requests a null pointer, and generate
the correctly-typed null pointer value. Therefore, the following
fragments are perfectly legal:

char *p = 0;
if(p != 0)

(See also question 5.3.)

However, an argument being passed to a function is not necessarily
recognizable as a pointer context, and the compiler may not be able
to tell that an unadorned 0 ``means'' a null pointer. To generate a
null pointer in a function call context, an explicit cast may be
required, to force the 0 to be recognized as a pointer. For example,
the Unix system call execl takes a variable-length,
null-pointer-terminated list of character pointer arguments, and is
correctly called like this:

execl("/bin/sh", "sh", "-c", "date", (char *)0);

If the (char *) cast on the last argument were omitted, the compiler
would not know to pass a null pointer, and would pass an integer 0
instead. (Note that many Unix manuals get this example wrong .)

When function prototypes are in scope, argument passing becomes an
``assignment context,'' and most casts may safely be omitted, since
the prototype tells the compiler that a pointer is required, and of
which type, enabling it to correctly convert an unadorned 0. Function
prototypes cannot provide the types for variable arguments in
variable-length argument lists however, so explicit casts are still
required for those arguments. (See also question 15.3.) It is safest
to properly cast all null pointer constants in function calls: to
guard against varargs functions or those without prototypes, to allow
interim use of non-ANSI compilers, and to demonstrate that you know
what you are doing. (Incidentally, it's also a simpler rule to
remember.)
Tommy 2003-04-01
  • 打赏
  • 举报
回复
C和C++中类型转换规则有所不同。

C++中,一个成员函数指针是不能转换为void*的,因此如果将NULL定义为(void*)0的话,成员函数指针就不能与NULL比较了。因此要定义为0。
  • 打赏
  • 举报
回复
由于C++的强类型转换,void*如果不经过转换是不能随便赋给其他指针的,因此象C中
#define NULL ((void*)0) 是无法使用通过编译的。
因此,空指针被定义为0
wyqiang 2003-04-01
  • 打赏
  • 举报
回复
那不一样
我还是没看出来
大 你怕#inndef吧


它可以简化为
#define NULL ((void*)0)
lbaby 2003-04-01
  • 打赏
  • 举报
回复
不知,
猜:有可能是因为NULL在c中也可以
作为函数出现

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧