根据此C代码,指针和变量的值是什么?

时间:2016-03-13 03:46:31

标签: c pointers

我对指针感到困惑....为什么ipp的值不是88?如果*ipp=88,它会显示垃圾? *ipp可以说同样的话吗?它还显示垃圾,为什么会这样?

你能解释一下我的情况下指针是如何工作的吗?为什么*ip1的值为97(如何从公式*ip1 * 7 + b开始)?

int a = 11, b = 20;
int x, y;

int *ip1 = &a;
int *ip2 = &x;

int **ipp = &ip2;

*ip2 = *ip1 * 7 + b;

ip1 = ip2;
ip2 = &y;

**ipp = 88;
*ipp = &b;

2 个答案:

答案 0 :(得分:2)

这里有一些代码可以显示正在发生的事情(非常难以理解):

#include <stdio.h>

int main(void)
{
    int a = 11;
    int b = 20;
    int x = -1;
    int y = -1;
    int *ip1 = &a;
    int *ip2 = &x;
    int **ipp = &ip2;

    printf("Addresses:\n");
    printf("   &a = %p\n", (void *)&a);
    printf("   &b = %p\n", (void *)&b);
    printf("   &x = %p\n", (void *)&x);
    printf("   &y = %p\n", (void *)&y);
    printf(" &ip1 = %p\n", (void *)&ip1);
    printf(" &ip2 = %p\n", (void *)&ip2);
    putchar('\n');
    printf("%3s  %3s  %3s  %3s  %14s  %14s  %14s  %4s  %4s  %5s\n",
           "a", "b", "x", "y", "ip1", "ip2", "ipp", "*ip1", "*ip2", "**ipp");
    printf("%3d  %3d  %3d  %3d  %14p  %14p  %14p  %4d  %4d  %5d\n",
           a, b, x, y, (void *)ip1, (void *)ip2, (void *)ipp, *ip1, *ip2, **ipp);

    *ip2 = *ip1 * 7 + b;
    printf("%3d  %3d  %3d  %3d  %14p  %14p  %14p  %4d  %4d  %5d\n",
           a, b, x, y, (void *)ip1, (void *)ip2, (void *)ipp, *ip1, *ip2, **ipp);

    ip1 = ip2;
    printf("%3d  %3d  %3d  %3d  %14p  %14p  %14p  %4d  %4d  %5d\n",
           a, b, x, y, (void *)ip1, (void *)ip2, (void *)ipp, *ip1, *ip2, **ipp);

    ip2 = &y;
    printf("%3d  %3d  %3d  %3d  %14p  %14p  %14p  %4d  %4d  %5d\n",
           a, b, x, y, (void *)ip1, (void *)ip2, (void *)ipp, *ip1, *ip2, **ipp);

    **ipp = 88;
    printf("%3d  %3d  %3d  %3d  %14p  %14p  %14p  %4d  %4d  %5d\n",
           a, b, x, y, (void *)ip1, (void *)ip2, (void *)ipp, *ip1, *ip2, **ipp);

    *ipp = &b;
    printf("%3d  %3d  %3d  %3d  %14p  %14p  %14p  %4d  %4d  %5d\n",
           a, b, x, y, (void *)ip1, (void *)ip2, (void *)ipp, *ip1, *ip2, **ipp);

    return 0;
}

它只打印除ipp之外的所有变量的地址 - 它的地址永远不会被代码中的任何表达式使用。然后它打印每个普通整数,每个指针和各种解除引用指针的值。

示例输出

Addresses:
   &a = 0x7fff5bacd4f0
   &b = 0x7fff5bacd4f4
   &x = 0x7fff5bacd4f8
   &y = 0x7fff5bacd4fc
 &ip1 = 0x7fff5bacd500
 &ip2 = 0x7fff5bacd508

  a    b    x    y             ip1             ip2             ipp  *ip1  *ip2  **ipp
 11   20   -1   -1  0x7fff5bacd4f0  0x7fff5bacd4f8  0x7fff5bacd508    11    -1     -1
 11   20   97   -1  0x7fff5bacd4f0  0x7fff5bacd4f8  0x7fff5bacd508    11    97     97
 11   20   97   -1  0x7fff5bacd4f8  0x7fff5bacd4f8  0x7fff5bacd508    97    97     97
 11   20   97   -1  0x7fff5bacd4f8  0x7fff5bacd4fc  0x7fff5bacd508    97    -1     -1
 11   20   97   88  0x7fff5bacd4f8  0x7fff5bacd4fc  0x7fff5bacd508    97    88     88
 11   20   97   88  0x7fff5bacd4f8  0x7fff5bacd4f4  0x7fff5bacd508    97    20     20

确保您了解所看到的内容 - 当所有数据都存在时,连接点并不困难。

答案 1 :(得分:1)

以下是这样做的:

int a = 11, b = 20;
int x, y;

int *ip1 = &a;
int *ip2 = &x;

int **ipp = &ip2;

此时a为11,b为20,ip1为&amp; a,ip2为&amp; x,ipp为&amp; ip2

*ip2 = *ip1 * 7 + b;

现在x是97,即11 * 7 + 20

ip1 = ip2;
ip2 = &y;

现在ip1是&amp; x,ip2是&amp; y

**ipp = 88;
*ipp = &b;

现在y是88,ip2是&amp; b