我只是无法理解这么简单的 program.Please 帮助。下面是程序和我的理解的表示

I am just not able to wrap my head around such a simple program.Please help. Below is the program and a representation of my understanding

#include <stdio.h>

int main()
{
   int x, y, *a, *b, temp;

   printf("Enter the value of x and y\n");
   scanf("%d%d", &x, &y);

   printf("Before Swapping\nx = %d\ny = %d\n", x, y);

   a = &x;
   b = &y;

   temp = *b;  
   *b = *a;    
   *a = temp;  

   printf("After Swapping\nx = %d\ny = %d\n", x, y);

   return 0;
}

Picture of my understanding

I understand that a pointer is a variable that holds the address of another
variable . But I am simply not able to wrap my head around this.Thanks in advance for the help .

你的逻辑有问题。

a = &x; makes pointer a point to address of x;
b = &y; makes pointer b point to address of y;

temp = *b;//set variable temp equal to the dereferenced value of b, *b implies the following: dereference b, which in this case means to obtain the value b is pointing to. temp value is y.

*b = *a; // set the value b points to equal to the value a points to.  Now y value equals to x.
*a = temp// dereference a and set the value a points to equal to temp.  Now x is set to y, and y was set to x previously thus x doesn't change.

这只是swapping using pointers

temp = *b;  
// *b is the value of y, here we store the value pointed to by b in temp
*b = *a;    
// *a is the value of x, here we store the value pointed to by a in y,
// here *b is nothing but y as noted in first step
*a = temp;  
// Here you understand the intention of using temp, the old value
// of y is no more, so use temp and we assign its value to *a ie x

实际上这就是正在发生的事情。假设你输入 1,2.

0x100000       0x100007
--------+-------+-------+---// --------+
        |       |       |
    1   |       |  2    |
--------+-------+-------+--// --------+
   x               y


a = &x;  // 0x100000
b = &y;  // 0x100007

----------------+-...+---------------+
                |    |               |
0x100000        |    |0x100007       |
----------------+-...+---------------+
    a                     b


temp = *a; // means go at the address that is contained by me and give me that value
// temp = 1;

*a = *b;  // put the value at address pointed by b to the address pointed by a

*b = temp; // put the value we kept in temp in the address pointed by b.

它是如何交换的?

问题是有两个变量 xy。现在当你想填写它时,你将 xy 的地址传递给 scanf()。为什么要传地址?因为只有访问那些地址 scanf() 才会写入变量。您可以在 main().

中查看更改

现在让我们看看接下来会发生什么:

问题是有两个变量

int*x, *y;

它们是整数指针。这些是什么?它们可以保存 int 变量的地址。好吧 intab 这样的变量。现在一切都很好。您想要获取 ab 的地址并存储它们。所以你写了

x=&a;
y=&b;

有了这个,你就拥有了 ab 的地址。

然后呢?

您引入了另一个变量。 temp 可以容纳 int 个值。

现在你写了

temp = *a;

也就是说,"hey, whatever address you have, fetch me the value that is in that address"。 ax 的地址。 x 包含 1temp 现在包含 1.

*a = *b;

同样,现在你说,写下你从 b 中包含的地址获得的值,并将该值放在某个地方。这个地方就是 a 中包含的地址。在那个地址你有 x 变量的内容。所以你刚刚对变量 x.

进行了更改
*b = temp;

好吧,这很相似。现在你在b中包含的地址写下temp中包含的值。 b中包含的地址是y的地址。您更改了 y 的值。

他们就是这样交换的。

图片有点不对。

假设 x 在地址 0x100h y 在地址 0x200h

最初

a points to 0x100h

同时

b points to 0x200h

在您的代码中,您交换了地址内的值,而不是 a 或 b 指向的地址。

但是你用来形象化的图片说你实际上交换了a和b指向的地址,这是错误的。试试下面的代码:

#include <stdio.h>

int main()
{
   int x, y, *a, *b, temp;

   printf("Enter the value of x and y\n");
   scanf("%d%d", &x, &y);

   printf("Before Swapping\nx = %d\ny = %d\n", x, y);
   printf("Address of x= %p and address of y= %p\n",(void*)&x,(void*)&y);
   a = &x;
   b = &y;
   printf("a holds the address: %p \nb holds the address %p\n",a,b);

   temp = *b;
   *b = *a;
   *a = temp;

   printf("After Swapping\nx = %d\ny = %d\n", x, y);
   printf("Address of x= %p and address of y= %p\n",(void*)&x,(void*)&y);
   printf("a holds the address: %p \nb holds the address %p\n",a,b);
   return 0;
}