gpt4 book ai didi

c - 为什么我应该使用双指针变量来接收另一个指针的地址(&ptr1)

转载 作者:太空狗 更新时间:2023-10-29 17:22:14 24 4
gpt4 key购买 nike

int num = 45,*ptr1,*ptr2;
ptr1=#
ptr2=&ptr1;
printf("%d\n",*ptr1);

这个问题想了好久,一直没找到办法理解,为什么&ptr1不能赋值给ptr2 3、&ptr1是一个指针的地址,这个地址和其他地址没有区别,比如一个整数的地址

int a=1;
ptr2=&a;

这意味着我可以将一个整数的地址分配给一个指针,但不能将一个指针的地址分配给一个指针,这两个“地址”之间有什么区别可能使它们不同?普通变量的地址可以赋给单指针,指针的地址不能赋给单指针?

我知道正确的做法是使用双指针来声明ptr2,但为什么单指针不能呢?

最佳答案

简单地说,指针不是地址,它们是代表地址和类型的变量。因此,这些类型与要分配的指针兼容(void * 通用指针除外)。

ptr2 = &ptr1;

ptr1 的类型是int *,所以&ptr1 的类型是int **,它是与 ptr2 不同,它的类型为 int *

引用:C99 6.5.16.1 简单赋值

both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left has all the qualifiers of the type pointed to by the right.

关于c - 为什么我应该使用双指针变量来接收另一个指针的地址(&ptr1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17680077/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com