[英]What does (char*) 0 mean?
This is a question in reference to this question: What does (char *)0 mean in c? 这是一个关于这个问题的问题: (char *)0在c中是什么意思?
There the answers slightly deviated away from explaining what exactly the answer was, but the final answer mentioned that it was a pointer to a character at address 0 and that it was null. 答案稍微偏离了解释究竟是什么答案,但最后的答案提到它是一个指向地址0处的字符的指针,它是空的。 This brought up two doubts for me.
这给我带来了两个疑问。
In c, can we give char* 9 and say that it is a pointer to address 9? 在c中,我们可以给char * 9并说它是指向地址9的指针吗? Won't we get any error or a warning?
我们不会收到任何错误或警告吗?
Ok let's say that (char*) 0 is indeed a pointer to character at address 0, but what does this address 0 mean? 好吧,假设(char *)0确实是指向地址0处字符的指针,但这个地址0是什么意思? I mean how can we say it's a null?
我的意思是我们怎么能说它是空的呢? In that case what would the value of (char*) 1 or (char*) 2 etc be?
在那种情况下,(char *)1或(char *)2等的值是多少?
Edit: Just to put it here whether it helps or not. 编辑:只是把它放在这里是否有帮助。 My initial search for this question occurred when I found out that the last argument in
execl
linux system call was null and I saw a rather odd looking syntax for it: (char *) 0. 当我发现
execl
linux系统调用中的最后一个参数为null并且我看到了一个相当奇怪的语法时,我最初搜索这个问题:(char *)0。
Thanks. 谢谢。
(char *) 0
is not a "pointer to a character at address 0". (char *) 0
不是“指向地址0处字符的指针”。 In C (char *) 0
is treated in a special way - it is guaranteed to produce a null-pointer value of type char *
. 在C
(char *) 0
以特殊方式处理 - 保证生成类型为char *
的空指针值 。 Formally it does not point to any char
object. 形式上它不指向任何
char
对象。 Its actual numerical value (the "address") is implementation-defined and can correspond to any address, not necessarily 0
. 它的实际数值(“地址”)是实现定义的,可以对应任何地址,不一定是
0
。 Eg numerically (char *) 0
can produce a pointer that "points" to address 0xFFFFFFFF
, for one example, if the given platform reserves this address for null-pointer value of char *
type. 例如,如果给定平台为
char *
type的空指针值保留该地址,则数字(char *) 0
可以产生指向地址0xFFFFFFFF
的指针。 But again, from the language point of view, a null-pointer value does not really point anywhere. 但同样,从语言的角度来看,空指针值并不能真正指向任何地方。
(char *) 9
does not have such special meaning. (char *) 9
没有这种特殊含义。 The pointer that (char *) 9
produces is also implementation-dependent. (char *) 9
产生的指针也与实现有关。 In most implementations it will indeed produce a char *
pointer to address 9
. 在大多数实现中,它确实会产生一个指向地址
9
的char *
指针。
In order to work around that special treatment of (char *) 0
you can try something like 为了解决
(char *) 0
特殊处理,你可以尝试类似的东西
int i = 0;
(char *) i;
The above (char *) i
(albeit implementation-dependent too) will usually produce a char *
pointer to address 0
. 上面的
(char *) i
(虽然也依赖于实现)通常会产生一个指向地址0
的char *
指针。 The key moment here that disables the special treatment is the fact that 0
in the expression is no longer a constant. 这里禁用特殊处理的关键时刻是表达式中的
0
不再是常数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.