[英]Understanding complex pointer and address-of operators using *&*&p in C
Can any one explain how the printf is printing hello in the following? 任何人都可以在下面解释printf如何打个招呼吗?
#include<stdio.h>
void main()
{
char *p;
p="hello";
printf("%s",*&*&p);
}
I know that *&p...means value in p, ie the address of string "hello". 我知道*&p ...表示p中的值,即字符串“ hello”的地址。 What is happening in the initial *&
初始*&中发生了什么
As you said, *&p
means p
, that means the consecutive *
and &
cancels out. 如您所说,
*&p
表示p
,表示连续的*
和&
抵消。 Hence *&*&p
becomes p
too. 因此,
*&*&p
变为p
。
And as @Kerrek said (in the comment) that *&p
produces an lvalue, so you take its address again . 正如@Kerrek所说(在评论中),
*&p
产生一个左值,因此您再次使用其地址。
Note that your code is not standard conformant. 请注意,您的代码不符合标准。
main()
must have int
as return type. main()
必须具有int
作为返回类型。 And you cannot assign "hello"
to a non-const char*
. 并且您不能将
"hello"
分配给非const char*
。 It must be const char*
. 它必须是
const char*
。 A standard conformant code would be this: 一个符合标准的代码是这样的:
#include<stdio.h>
int main()
{
const char *p = "hello";
printf("%s",*&*&p);
}
&p
is the address of p
. &p
是的地址p
。
*p
is the thing pointed at by the address p
. *p
是地址p
指向的东西。
*&p
is *(&p)
the thing pointed at by the address &p
- which is p
itself (ie, the thing pointed at by the address "address of p"). *&p
是*(&p)
由地址&p
指向的事物-它本身就是p
(即,由地址“ p的地址”指向的事物)。
Thus it turns out that *&p
is just p
- the *&
cancel each other out. 因此,事实证明
*&p
只是p
- *&
互相抵消。 You can repeat this: *&*&p
will still be p
. 您可以重复一遍:
*&*&p
仍为p
。 You can do this ad infinitum: *&*&*&*&*&*&*&*&*&p
will also be p
. 您可以无限地制作此广告:
*&*&*&*&*&*&*&*&*&p
也将是p
。
'*&' cancels each other. '*&'互相抵消。 You are getting the address of p then dereferencing it again.
您将获取p的地址,然后再次对其取消引用。 So the end result will just be p.
因此,最终结果将仅为p。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.