|
#include<iostream>
#include<string>
using namespace std;
struct Student
{
string name;
int xh, cj;
};
int main()
{
Student group[5];
Student* pgroup = group;//-----------------1
cout << pgroup << endl;
pgroup = group;//-----------------------------2
cout << pgroup<<endl;
cout << "请依次输入学生的’姓名‘,’学号’,‘成绩’"<<endl;
//获取信息
for (int i = 0; i < 5; i++,pgroup++)
{
cin >> pgroup->name >> pgroup->xh >> pgroup->cj;
if (i == 4)
{
pgroup = group;//-----------------------------------3
}
}
cout << pgroup<<endl;
cout << &group[1]<<endl;//----------------------------------------4
return 0;
}
这段代码我输出后发现的1,2的结构体地址是一样的,3,4的地址是一样的,但不应该1,2,3的地址都是一样的么,然后4和1,2,3都不一样
就解答
最后的结果不一样的原因是:
你内嵌的if(i == 4){
pgroup = group;
}
判断结束后,
外部的 for(int i = 0;i < 5; i++,pgroup++)
i=4 判断结束, 此时进行最后判断
因为i=4,所以 i<5成立 ,
i++,pgroup++ 继续执行
i = 5, pgroup = pgroup+4(也就是变成你所说的那个 第二个结构体数组group[1]的地址了 )
|
上一篇: 远程卸载DLL 导致目标程序崩溃问题。下一篇: 36课作业请求帮助
|