* y ++和++ * y之间的区别? [英] difference between *y++ and ++*y?

查看:1344
本文介绍了* y ++和++ * 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屋!

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