首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

为什么第四个输出的是1004 啊 帮忙解释一下

2012-06-23 
求助:为什么第四个输出的是1004 啊帮忙解释一下#include iostreamusing namespace stdstruct Data{ int

求助:为什么第四个输出的是1004 啊 帮忙解释一下
#include <iostream>
using namespace std;
struct Data
 { int n ;
  double score ;
 } ;
int main()
{ Data a[3] = { 1001,87,1002,72,1003,90 } , *p = a ;
  cout << (p++)->n << endl ;
  cout << (p++)->n << endl ;
  cout << p->n++ << endl ;
  cout << (*p).n++ << endl ;
}
为什么第四个输出的是1004 啊 帮忙解释一下

[解决办法]
#include <iostream>
using namespace std;
struct Data
 { int n ;
double score ;
 } ;
int main()
{ Data a[3] = { 1001,87,1002,72,1003,90 } , *p = a ;
cout << (p++)->n << endl ;
// p is point to { 1001,87,1002,72,1003,90 }
cout << (p++)->n << endl ;
// p is point to { 1001,87,1002,72,1003,90 }
cout << p->n++ << endl ;
// p is point to { 1001,87,1002,72,1003,90 } and add this number when use it next time 
cout << (*p).n++ << endl ;
// p is still at the position { 1001,87,1002,72,1003,90 } as the resaon of n++ below , the the n is 1004 , you can use 
 cout << (*p).n < endl ; to see at that time the result is 1005;
}

热点排行
Bad Request.