间接更新变量

时间:2017-02-25 21:15:00

标签: c pointers

我正在学习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的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;
}

有人能告诉我我做错了什么吗?我没有看到该计划有什么问题。

3 个答案:

答案 0 :(得分:2)

如果新变量将保留x的地址,则它必须是a 指向x int **类型的指针。所以:

int ***newVariable;

您正确地分配了x的地址,其中包含:

newVariable = &x;

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

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

要打印i的值,您可以直接使用自己,或p 指向它,或x指向pnewVariable 指向x。因此,这些陈述将产生相同的结果:

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

答案 1 :(得分:2)

您不应在一个文件中提供两个main()函数。

第一个问题是newVariable定义:

int ** x; /* pointer to pointer */
/* Since you want to store adress of x, you should declare pointer to pointer to pointer */
int *** newVariable; /* variable to store adress of x */

第二个问题是如何间接递增i值:

/* Since newVariable -> x -> p -> i (where "->" means "is a pointer to") */
(***newVariable)++; /* increment value of i by 1 */

答案 2 :(得分:1)

您需要使用*** newVar并指向x的地址。然后您可以更改newVar的指向值并显示x也已更改。

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

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

    // prove that newVar is pointing to same as x
    printf("**x= %d\n", **x);                 //100
    ***newVar = 12345;
    printf("***newVar= %d\n", ***newVar);     //12345
    printf("**x= %d\n", **x);                 //12345
    printf("*p= %d\n", *p);                   //12345
    printf("i= %d\n", i);                     //12345

    //prove that they are all pointing to same address
    printf("p= %p\n", p);                     
    printf("*x= %p\n", *x);
    printf("**newVar= %p\n", **newVar);   


    return 0;
}