题目:建立一个对象数组,内放5个学生的数据(学号,成绩) ,用指针指向数组首元素,输出第1、3、5个
时间: 2024-05-12 17:17:47 浏览: 93
学生的学号和成绩。
答案:
Sure, here's an example code:
```c++
#include <iostream>
using namespace std;
struct Student {
int id;
int score;
};
int main() {
Student students[5] = {
{101, 85},
{102, 92},
{103, 76},
{104, 89},
{105, 78},
};
Student *p = students;
cout << "The 1st student's ID is " << p->id << " and the score is " << p->score << endl;
cout << "The 3rd student's ID is " << (p + 2)->id << " and the score is " << (p + 2)->score << endl;
cout << "The 5th student's ID is " << (p + 4)->id << " and the score is " << (p + 4)->score << endl;
return 0;
}
```
Output:
```
The 1st student's ID is 101 and the score is 85
The 3rd student's ID is 103 and the score is 76
The 5th student's ID is 105 and the score is 78
```
Hope this helps!
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)