无法理解涉及指针C程序中的一小部分 [英] Can't understand small part of a C program involving pointers

查看:130
本文介绍了无法理解涉及指针C程序中的一小部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  INT X = 5;
INT Y = 6;
为int * p =&放大器; X;
* P = X + Y;
P =&安培; Y;
* P = X + Y;
的printf(%D,%D,X,Y);

我能理解第3行。我知道,为int * p =&放大器; X; 表示指针 P 是在内存地址指向 X 。但我无法理解接下来的4行code的。此code的输出是:

  11,17

但我无法理解怎么样?有人可以解释给我。


解决方案

  

指针是一个变量,其值是另一个变量的地址,即内存位置的直接地址。


当你分配这样的,

 为int * p =&放大器; X; //这将指向x变量的地址。

* P做任何更改,它会影响记忆力。所以当你访问一个与 X 它具有的价值。

  * P = X + Y; //这相当于X = X + Y;

然后也是这样。

现在 X 的值为 11 P =&安培; Y; ​​

  * P = X + Y; //它等同于Y = X + Y;

所以现在x的值 11 和y值 5 。所以结果是 17

int x = 5;
int y = 6;
int *p = &x;
*p = x + y;
p = &y;
*p = x + y;
printf("%d,%d", x, y);

I can understand the first 3 lines. I know that int *p = &x; means that the pointer p is pointing at the memory address of x. But I can't understand the next 4 lines of the code. The output for this code is:

 11,17

But I unable to understand how? Can someone explain it to me.

解决方案

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location.

When you are assigning like this,

 int *p=&x;// It will point to the address of x variable. 

Any change done in the *p, It will affect the memory. So while you are accessing that with the x it have the value.

 *p=x+y; // It's equivalent x=x+y;

Then y also like this.

Now x have the value 11. p=&y;

  *p=x+y;// It's equivalent to y=x+y;

so now x have the value 11 and y value 5. So the result is 17.

这篇关于无法理解涉及指针C程序中的一小部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆