gpt4 book ai didi

c - 这两种指针方法的区别

转载 作者:太空宇宙 更新时间:2023-11-04 05:13:20 25 4
gpt4 key购买 nike

解码1:

void decode1(int *xp, int *yp, int *zp){
int x = *xp;
int y = *yp;
int z = *zp;
xp = &z;
yp = &x;
zp = &y;
}

解码2:

 void decode2(int *xp, int *yp, int *zp){
int x = *xp;
int y = *yp;
int z = *zp;
*xp = z;
*yp = x;
*zp = y;
}

Decode1 会将指针更改为 z、x 和 y 的地址。 Decode2 将改为更改指针地址处的值。这两种方法可以互换吗?有没有一种情况比另一种更正确?

最佳答案

Decode2 是正确的程序。在 Decode1 中,调用栈结束后,你在 xp,yp,zp 中分配的地址将消失。

关于c - 这两种指针方法的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21668007/

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