* y ++和++ * y之间的区别? [英] difference between *y++ and ++*y?
问题描述
我对如何执行此代码感到困惑.假设我们有
I'm confused in how this code will get executed. Suppose we have
int x=30,*y,*z;
y=&x;
* y ++和++ * y有什么区别?以及该程序的输出是什么?
what is the difference between *y++ and ++*y? and also what will be the output of this program?
#include<stdio.h>
int main(){
int x=30,*y,*z;
y=&x;
z=y;
*y++=*z++;
x++;
printf("%d %d %d ",x,y,z);
return 0;
}
推荐答案
表达式x = *y++
的作用与以下内容相同:
The expression x = *y++
is in effects same as:
x = *y;
y = y + 1;
如果表达式只是*y++;
(未分配),则它与y++;
相同,即y
在递增后开始指向下一个位置.
And if expression is just *y++;
(without assignment) then its nothing but same as y++;
, that is y
start pointing to next location after increment.
第二个表达式++*y
表示增加y
所指向的值,与*y = *y + 1;
相同(指针未递增)
回答第一个问题会更清楚:
Second expression ++*y
means to increment the value pointed by y
that same as: *y = *y + 1;
(pointer not incremented)
It will be better clear with answer to your first question:
假设您的代码是:
int x = 30, *y;
int temp;
y = &x;
temp = *y++; //this is same as: temp = *y; y = y + 1;
第一个*y
将分配给temp
变量;因此,将temp
分配为30
,然后y
的值将增加1,并且它开始指向x
位置之后的下一个位置(实际上不存在任何变量).
First *y
will be assigned to temp
variable; hence temp
assigned 30
, then value of y
increments by one and it start point to next location after location of x
(where really no variable is present).
下一种情况:假设您的代码是:
Next case: Suppose your code is:
int x = 30, *y;
int temp;
y = &x;
temp = ++*y; //this is same as *y = *y + 1; temp = *y;
*y
的第一个值从30
递增到31
,然后将31
分配给temp
(注意:x
现在为31
).
First value of *y
increments from 30
to 31
and then 31
is assigned to temp
(note: x
is now 31
).
问题的下一部分(阅读评论):
next part of your question (read comments):
int x = 30, *y, *z;
y = &x; // y ---> x , y points to x
z = y; // z ---> x , z points to x
*y++ = *z++; // *y = *z, y++, z++ , that is
// x = x, y++, z++
x++; // increment x to 31
这篇关于* y ++和++ * y之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!