C++学习日志 15
结构体指针
作用:通过指针访问结构体中的成员
利用操作符->可以通过结构体指针访问结构体属性
示例:
#include<iostream>
using namespace std;
#include<string>
struct Student {
string name;
int age;
int score;
};
int main() {
//创建学生结构体变量
Student s= {"张三",18,100};
//通过指针指向结构体变量
Student* p = &s;
//通过指针访问结构体变量中的数据
cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
system("pause");
return 0;
}
作用:通过指针访问结构体中的成员
利用操作符->可以通过结构体指针访问结构体属性
示例:
#include<iostream>
using namespace std;
#include<string>
struct Student {
string name;
int age;
int score;
};
int main() {
//创建学生结构体变量
Student s= {"张三",18,100};
//通过指针指向结构体变量
Student* p = &s;
//通过指针访问结构体变量中的数据
cout << "姓名:" << p->name << " 年龄:" << p->age << " 分数:" << p->score << endl;
system("pause");
return 0;
}
全部评论
相关推荐
06-12 19:52
吉首大学张家界学院 Python 点赞 评论 收藏
分享