指向指针和指向数组的指针之间的区别?
鉴于数组的名称实际上是指向数组第一个元素的指针,以下代码:
#include <stdio.h>
int main(void)
{
int a[3] = {0, 1, 2};
int *p;
p = a;
printf("%d\n", p[1]);
return 0;
}
按预期打印 1
。
现在,考虑到我可以创建一个指向指针的指针,我编写了以下内容:
#include <stdio.h>
int main(void)
{
int *p0;
int **p1;
int (*p2)[3];
int a[3] = {0, 1, 2};
p0 = a;
p1 = &a;
p2 = &a;
printf("p0[1] = %d\n(*p1)[1] = %d\n(*p2)[1] = %d\n",
p0[1], (*p1)[1], (*p2)[1]);
return 0;
}
我期望它能够编译并打印,
p0[1] = 1
(*p1)[1] = 1
(*p2)[1] = 1
但相反,它在编译时出错,说:
test.c: In function ‘main’:
test.c:11:5: warning: assignment from incompatible pointer type [enabled by default]
为什么分配错误?如果 p1
是指向 int
的指针,而 a
是指向 int
的指针(因为它是int
数组的名称),为什么我不能将 &a
分配给 p1
?
Given that the name of an array is actually a pointer to the first element of an array, the following code:
#include <stdio.h>
int main(void)
{
int a[3] = {0, 1, 2};
int *p;
p = a;
printf("%d\n", p[1]);
return 0;
}
prints 1
, as expected.
Now, given that I can create a pointer that points to a pointer, I wrote the following:
#include <stdio.h>
int main(void)
{
int *p0;
int **p1;
int (*p2)[3];
int a[3] = {0, 1, 2};
p0 = a;
p1 = &a;
p2 = &a;
printf("p0[1] = %d\n(*p1)[1] = %d\n(*p2)[1] = %d\n",
p0[1], (*p1)[1], (*p2)[1]);
return 0;
}
I expected it to compile and print
p0[1] = 1
(*p1)[1] = 1
(*p2)[1] = 1
But instead, it goes wrong at compile time, saying:
test.c: In function ‘main’:
test.c:11:5: warning: assignment from incompatible pointer type [enabled by default]
Why is that assignment wrong? If p1
is a pointer to a pointer to an int
and a
is a pointer to an int
(because it's the name of an array of int
s), why can't I assign &a
to p1
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第 11 行是
p1
的类型为int **
且a
的类型为int[3]
,对吗?出色地;
&a
具有类型int(*)[3]
并且该类型与int**
不兼容,正如编译器告诉您的那样您可能想要尝试
并阅读c-faq,特别是第6节。
简而言之:数组不是指针< /strong> 和指针不是数组。
Line 11 is
where
p1
has typeint **
anda
has typeint[3]
, right?Well;
&a
has typeint(*)[3]
and that type is not compatible withint**
as the compiler told youYou may want to try
And read the c-faq, particularly section 6.
In short: arrays are not pointers, and pointers are not arrays.
a
不是指向int
的指针,它在某些情况下会衰减。如果&a
的类型为int **
,您就不能很好地使用它来初始化p2
,可以吗?您需要执行
p1 = &p0;
以获得您想要的效果。 “指向指针的指针”的意思是“在这个地址,你会找到一个指针”。但是,如果您查看地址&a
,您会发现一个数组(显然),因此int **
不是正确的类型。a
is not a pointer toint
, it decays to such in certain situations. If&a
was of typeint **
you couldn't very well use it to initializep2
, could you?You need to do
p1 = &p0;
for the effect you want. "pointer to pointer" means "at this address, you will find a pointer". But if you look at the address&a
, you find an array (obviously), soint **
is not the correct type.对于许多操作,
a
意味着&a
并且都返回相同的内容:数组中第一项的地址。您无法获取指针的地址,因为变量不存储指针。
a
不是一个指针,尽管它在某些情况下表现得像一个指针。For many operations,
a
implies&a
and both return the same thing: The address of the first item in the array.You cannot get the address of the pointer because the variable does not store the pointer.
a
is not a pointer, even though it behaves like one in some cases.