免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: ljttlf

null 指针的使用 [复制链接]

论坛徽章:
0
发表于 2004-09-19 02:11 |显示全部楼层

null 指针的使用

为什么不是这个情况?那什么情况才能满足你的要球?
我也没有说那是变参函数。

既然编译器知道哪个是指针,那你告诉我,

为什么
execl("/bin/sh", "sh", "-c", "date", (char *)0);
而不是
execl("/bin/sh", "sh", "-c", "date", NULL);
也不是
execl("/bin/sh", "sh", "-c", "date", (void*)0);
不是
execl("/bin/sh", "sh", "-c", "date", 0);

你可不要告诉我,(char*)和(void*)是一样的这样的解释。

论坛徽章:
0
发表于 2004-09-19 02:13 |显示全部楼层

null 指针的使用

>;>;c 语言规定,在指针上下文,常数 0 被自动转化为 null 指针常数。所以 foo("a", 0) 是完全合法的,符合标准的,没一点点错误

但是这里“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”


注意is not necessarily recognizable as a pointer context。

论坛徽章:
0
发表于 2004-09-19 02:16 |显示全部楼层

null 指针的使用

原文是这样的,对吗?

C programmers must understand that NULL and 0 are interchangeable in pointer contexts, and that an uncast 0 is perfectly acceptable

在 execl 里,不是 pointer contexts 啊!

注意in pointer contexts

论坛徽章:
0
发表于 2004-09-19 02:17 |显示全部楼层

null 指针的使用

不是,
你可以看看原文,http://www.eskimo.com/~scs/C-faq/q5.2.html

论坛徽章:
0
发表于 2004-09-19 02:19 |显示全部楼层

null 指针的使用

原帖由 "ljttlf" 发表:
为什么不是这个情况?那什么情况才能满足你的要球?
我也没有说那是变参函数。


我倒, execl 不是参数可变,那什么才是? man 一下。

论坛徽章:
0
发表于 2004-09-19 02:21 |显示全部楼层

null 指针的使用

我说我的foo不是:)

而且,原文讲解的时候,并没有将一定要是可变参数的现在列在里面阿.

论坛徽章:
0
发表于 2004-09-19 02:21 |显示全部楼层

null 指针的使用

1、如果参数个数固定,有声明,编译器知道是指针。
2、如果没声明,编译器不知道指针。这时不能直接用 0
3、如果有声明,但参数个数可变,则变化的参数跟没声明一样,编译器不知道它是个指针。

你的 foo 声明了, 所以直接用0 可以。execl 参数是可变的,直接用 0 不行。

论坛徽章:
0
发表于 2004-09-19 02:24 |显示全部楼层

null 指针的使用

>;>;在 execl 里,不是 pointer contexts 啊!

对阿,要是在pointer contexts里面当然直接使用0不会有什么问题,但是这里不是pointer contexts阿,而是function call contexts了阿(我开始把NULL记成c++的定义了,所以直接说成0 了,呵呵 ,那我们现在讨论0吧,反正在指针环境里面,0,和(void*)0一样。

论坛徽章:
0
发表于 2004-09-19 02:25 |显示全部楼层

null 指针的使用

5.2的意思是说,有函数调用现在就是function call context,不再是pointer contexts了阿

论坛徽章:
0
发表于 2004-09-19 02:26 |显示全部楼层

null 指针的使用

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.

从这句话里面,你能得出
>;>;
1、如果参数个数固定,有声明,编译器知道是指针。
2、如果没声明,编译器不知道指针。这时不能直接用 0
3、如果有声明,但参数个数可变,则变化的参数跟没声明一样,编译器不知道它是个指针。

function call context也需要满足这些要求吗?

那你说foo()是 points contexts还是function call contexts?为什么?

如果对于没有修饰转型的0,   points contexts可以直接使用,function call contexts不能直接使用,那么如果是points contexts 和 function call contexts 两个状态的交集怎么定义呢?到底能还是不能直接使用0?

如果foo()中的0的使用,只依赖于是否是points contexts状态,而与function call contexts无关的话,那我承认是我的错。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP