gpt4 book ai didi

c - 间接更新变量

转载 作者:太空狗 更新时间:2023-10-29 16:10:37 25 4
gpt4 key购买 nike

我正在学习 C,我被分配了一项任务。

“通过添加一个存储 x 地址的新变量来修改程序。然后使用您的变量更新(间接)i 的值,然后打印出新值以证明您的修改已生效。”这是我必须修改的代码:

#include <stdio.h>
int main()
{
int i, j;
int * p, * q;
int ** x;

i = 100;
j = 200;
p = &i;
q = &j;
x = &p;

*p = *p + *q;
*q = **x / 2;
**x = *p + j;

printf(" i = %d\n", i);
printf("&i = %p\n", &i);
printf(" j = %d\n", j);
printf("&j = %p\n", &j);

printf(" p = %p\n", p);
printf("&p = %p\n", &p);
printf("*p = %d\n", *p);
printf(" q = %p\n", q);
printf("&q = %p\n", &q);
printf("*q = %d\n", *q);

printf(" x = %p\n", x);
printf("&x = %p\n", &x);
printf("*x = %p\n", *x);
printf("**x= %d\n", **x);
return 0;
}

这是我尝试过的。我声明了存储地址的变量。然后我给它分配了 x of x (newVariable) 的地址,然后尝试创建地址 i 的增量更新。编译程序时出现以下错误:

ptr3.c:14:18: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
*newVariable = &x; /* Assign new variable address of x */
^
ptr3.c:19:19: error: lvalue required as unary ‘&’ operand
newVariable = &i++; /* Autoincrement address by 1 */
^
ptr3.c:21:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
printf("&newVariable = %d\n", newVariable); /* New printf statement */
^

代码:

#include <stdio.h>
int main()
{
int i, j;
int * p, * q;
int ** x;
int * newVariable; /* New variable of type address*/

i = 100;
j = 200;
p = &i;
q = &j;
x = &p;
newVariable = &x; /* Assign new variable address of x */

*p = *p + *q;
*q = **x / 2;
**x = *p + j;
newVariable = &i++; /* Autoincrement address by 1 */

printf("&newVariable = %d\n", newVariable); /* New printf statement */

printf(" i = %d\n", i);
printf("&i = %p\n", &i);
printf(" j = %d\n", j);
printf("&j = %p\n", &j);

printf(" p = %p\n", p);
printf("&p = %p\n", &p);
printf("*p = %d\n", *p);
printf(" q = %p\n", q);
printf("&q = %p\n", &q);
printf("*q = %d\n", *q);

printf(" x = %p\n", x);
printf("&x = %p\n", &x);
printf("*x = %p\n", *x);
printf("**x= %d\n", **x);

return 0;
}

版本 3:

#include <stdio.h>
int main()
{
int i, j;
int * p, * q;
int ** x;
int * newVariable; /* New variable of type address*/

i = 100;
j = 200;
p = &i;
q = &j;
x = &p;
newVariable = &x; /* Assign new variable address of x */

*p = *p + *q;
*q = **x / 2;
**x = *p + j;
newVariable = &i++; /* Autoincrement address by 1 */

printf("&newVariable = %d\n", newVariable); /* New printf statement */

printf(" i = %d\n", i);
printf("&i = %p\n", &i);
printf(" j = %d\n", j);
printf("&j = %p\n", &j);

printf(" p = %p\n", p);
printf("&p = %p\n", &p);
printf("*p = %d\n", *p);
printf(" q = %p\n", q);
printf("&q = %p\n", &q);
printf("*q = %d\n", *q);

printf(" x = %p\n", x);
printf("&x = %p\n", &x);
printf("*x = %p\n", *x);
printf("**x= %d\n", **x);

return 0;
}

谁能告诉我我做错了什么?我看不出程序有什么问题。

最佳答案

如果新变量将保存 x 的地址,它需要是一个指向 x 类型的指针,它是 int **。所以:

int ***newVariable;

您正在正确分配 x 的地址,其中:

newVariable = &x;

要修改i,让我们稍微想一想。原来的x本身指向指向指向 i 的其他内容(在本例中为 p)。所以newVariable 添加了一个额外的级别,这意味着您只需要取消对它的三个级别的引用以访问 i。例如,如果你想要通过递增来修改 i,然后可以使用:

***newVariable += 3; // increment i (indirectly) by 3

要打印 i 的值,你可以直接使用它本身,或者 p which指向它,或者 x 指向 p,或者 newVariable指向 x。所以这些语句将产生相同的结果:

printf("i = %d\n", i);
printf("i = %d\n", *p);
printf("i = %d\n", **x);
printf("i = %d\n", ***newVariable);

关于c - 间接更新变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42461670/

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