请教以下程序为什么输出结果不一样??

性能侠客行 2008-03-11 07:52:04
程序1:
void main()
{
int *x= NULL;
int y[]={12,45,36,78,80};
x = y+2;
cout<<"----------*x="<<*x<<endl;
*(x++) = (*x)++;//疑问语句
cout<<"*x="<<*x<<'\n'<<"*y="<<*y<<endl;
cout<<"y[0]="<<y[0]<<endl;cout<<"y[1]="<<y[1]<<endl;cout<<"y[2]="<<y[2]<<endl;cout<<"y[3]="<<y[3]<<endl;
}
程序2:
void main()
{
int *x= NULL;
int y[]={12,45,36,78,80};
x = y+2;
cout<<"----------*x="<<*x<<endl;
(*x++)++;//疑问语句
cout<<"*x="<<*x<<'\n'<<"*y="<<*y<<endl;
cout<<"y[0]="<<y[0]<<endl;cout<<"y[1]="<<y[1]<<endl;cout<<"y[2]="<<y[2]<<endl;cout<<"y[3]="<<y[3]<<endl;
}
程序3:
void main()
{
int *x= NULL;
int y[]={12,45,36,78,80};
x = y+2;
cout<<"----------*x="<<*x<<endl;
(*++x)++;//疑问语句
cout<<"*x="<<*x<<'\n'<<"*y="<<*y<<endl;
cout<<"y[0]="<<y[0]<<endl;cout<<"y[1]="<<y[1]<<endl;cout<<"y[2]="<<y[2]<<endl;cout<<"y[3]="<<y[3]<<endl;
}
...全文
158 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
性能侠客行 2008-03-12
  • 打赏
  • 举报
回复


引用楼上
*(x++) = (*x)++;//首先执行x++,既是x指向y[3],然后给y[3]赋值为*(x)++,因为此时*(x)=y[3]=78,所以y[3]被赋值为79

但是如果换上语句,
(*x++)++;//疑问语句
或者是语句
(*(x++))++;
//为什么不首先执行x++,然后再进行取值呢??


*(x++) = (*x)++;
与(*x++)++;的区别???

请指点,tks!
chenccq 2008-03-12
  • 打赏
  • 举报
回复
void main()
{
int *x= NULL;
int y[]={12,45,36,78,80};
x = y+2; //x指针指向y[2],y[2]=36
cout < <"----------*x=" < <*x < <endl;
*(x++) = (*x)++;//首先执行x++,既是x指向y[3],然后给y[3]赋值为*(x)++,因为此时*(x)=y[3]=78,所以y[3]被赋值为79
cout < <"*x=" < <*x < <'\n' < <"*y=" < <*y < <endl;
cout < <"y[0]=" < <y[0] < <endl;cout < <"y[1]=" < <y[1] < <endl;cout < <"y[2]=" < <y[2] < <endl;cout < <"y[3]=" < <y[3] < <endl;
}
性能侠客行 2008-03-12
  • 打赏
  • 举报
回复
程序1:
void main()
{
int *x= NULL;
int y[]={12,45,36,78,80};
x = y+2;
cout < <"----------*x=" < <*x < <endl;
*(x++) = (*x)++;//疑问语句
cout < <"*x=" < <*x < <'\n' < <"*y=" < <*y < <endl;
cout < <"y[0]=" < <y[0] < <endl;cout < <"y[1]=" < <y[1] < <endl;cout < <"y[2]=" < <y[2] < <endl;cout < <"y[3]=" < <y[3] < <endl;
}

*(x++) = (*x)++;//疑问语句
这句话有比较疑惑,感觉怪怪的,
请高手指教,输出结果为:
----------*x=36
*x=79
*y=12
y[0]=12
y[1]=45
y[2]=36
y[3]=79
Press any key to continue
joinnycoo 2008-03-11
  • 打赏
  • 举报
回复
X++和++X是不一样的;
前者是先判断在加一,后者是先加一在判断。
dubiousway 2008-03-11
  • 打赏
  • 举报
回复
lz 知道 *y 和 *(y+1)的值吗?

69,379

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧