通过指针对结构体成员对象的访问输出C/C++



#include <iostream>
using namespace std;
//定义一个结构体用于保存函数的信息参数
typedef struct
{   int id;   //学生的学号
    float math;          //数学
    float physics;  //物理
    float computer;  //计算机
    float chemistry;  //化学
    float english;  //英语
    float political;  //政治
    float sport;   //休育
    float history;  //历史
    float geography;     //地理
} STUDENT_PARMS;
//声明函数print_student_report的原型
void print_student_report(STUDENT_PARMS *);
//主程序
int main(void)
{   STUDENT_PARMS student_parm;         //实例化学生成绩结构体对象
    student_parm.math = 99;  //数学
    student_parm.physics = 80;  //物理
    student_parm.computer = 100; //计算机
    student_parm.chemistry = 76; //化学
    student_parm.english = 86;  //英语
    student_parm.political = 60; //政治
    student_parm.sport = 90;  //休育
    student_parm.history = 75;  //历史
    student_parm.geography = 90; //地理
    print_student_report(&student_parm); 
//通过指向对象的指针参数来打印学生的成绩
    return 1;
}
//定义一个能够通过指针打印出学生全部成绩的函数
void print_student_report(STUDENT_PARMS *p)
{   //输出学生的学号
    cout << "student id is: ";
    cout << (p->id) << "\n"; //注意通过指针来访问结构体成员的方式
    //输出数学分数
    cout << "math score is: " ;
    cout << (p->math) << "\n";
    //输出物理分数
    cout << "physics score is: ";
    cout << (p->physics) << "\n";
    //输出计算机分数
    cout << "computer score is: ";
    cout << (p->computer) << "\n";
    //输出化学分数
    cout << "chemistry score is: ";
    cout << (p->chemistry) << "\n";
    //输出英语分数
    cout << "english score is: ";
    cout << (p->english) << "\n";
    //输出政治分数
    cout << "political score is: ";
    cout << (p->political) << "\n";
    //输出体育成绩
    cout << "sport score is: ";
    cout << (p->sport) << "\n";
    //输出历史分数
    cout << "history score is: ";
    cout << (p->history) << "\n";
    //输出地理成绩
    cout << "geography score is: ";
    cout << (p->geography) << "\n";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值