建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据
时间: 2023-05-29 17:07:03 浏览: 79
#include <iostream>
using namespace std;
struct Student{
int id;
float score;
};
int main(){
Student stu[5] = {{1, 90.5}, {2, 85}, {3, 77.5}, {4, 92}, {5, 88}};
Student *p = stu;
cout << "第1个学生的学号为:" << p->id << ", 成绩为:" << p->score << endl;
cout << "第3个学生的学号为:" << (p+2)->id << ", 成绩为:" << (p+2)->score << endl;
cout << "第5个学生的学号为:" << (p+4)->id << ", 成绩为:" << (p+4)->score << endl;
return 0;
}
相关问题
建立一个对象数组,内放5个学生的学号和成绩,用指针指向数组的首元素,输出第1,3,5学生的数据。用C++实现
下面是用C++实现的代码:
```cpp
#include <iostream>
using namespace std;
struct Student {
int id; // 学号
int score; // 成绩
};
int main() {
Student students[5] = {
{1001, 85},
{1002, 92},
{1003, 78},
{1004, 90},
{1005, 88}
}; // 定义并初始化学生数组
Student* p = students; // 使用指针指向数组的首元素
cout << "第1个学生的数据:" << endl;
cout << "学号:" << p->id << ",成绩:" << p->score << endl;
p += 2; // 指向第3个学生的数据
cout << "第3个学生的数据:" << endl;
cout << "学号:" << p->id << ",成绩:" << p->score << endl;
p += 2; // 指向第5个学生的数据
cout << "第5个学生的数据:" << endl;
cout << "学号:" << p->id << ",成绩:" << p->score << endl;
return 0;
}
```
输出结果:
```
第1个学生的数据:
学号:1001,成绩:85
第3个学生的数据:
学号:1003,成绩:78
第5个学生的数据:
学号:1005,成绩:88
```
建立一个对象数组,内放5个学生数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。
在C++中,你可以创建一个动态数组`Student`,每个`Student`包含学号和成绩两个属性。首先,你需要定义一个`Student`结构体:
```cpp
struct Student {
int studentId;
float score;
};
```
然后,你可以使用`new`关键字动态分配内存来存储5个学生的数据:
```cpp
int numStudents = 5;
Student* students = new Student[numStudents];
// 初始化学生数据
students[0].studentId = 1; // 第一个学生学号
students[0].score = 85.0f;
students[1].studentId = 2;
students[1].score = 90.0f;
// ... 继续初始化其他学生
students[4].studentId = 5;
students[4].score = 95.0f;
```
接着,你可以使用指针遍历并输出第1,3,5个学生的数据:
```cpp
// 输出第一个学生数据
cout << "Student 1: ID - " << students[0].studentId << ", Score - " << students[0].score << endl;
// 输出第三个学生数据 (因为索引从0开始,所以第3个学生是index=2)
cout << "Student 3: ID - " << students[2].studentId << ", Score - " << students[2].score << endl;
// 输出第五个学生数据
cout << "Student 5: ID - " << students[4].studentId << ", Score - " << students[4].score << endl;
// 释放内存
delete[] students;
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.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)
![](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)