//通过指针变量访问并输出结构数组的各元

本文介绍了一种通过指针变量访问并输出C++结构数组各元素的方法,展示了如何利用指针进行高效的数据操作。
//通过指针变量访问并输出结构数组的各元素
#include<iostream.h>
#include<conio.h>

#define student__num 5 
struct student
{
   int num;
   char name[20];
   char sex;
   int score;       
       
       
};
void displaystudentinfo(const student *const);

int main()
{
  student theclass[student__num]={
                                 {110,"wei shanshan",'M',98},
                                 {120,"bai yushuang",'N',78},  
                                 {130,"liu linlinff",'N',89.5}, 
                                 {140,"xiao haizi",'M',78.5}, 
                                 {150,"cao jingjing",'M',69.7},
                                 };
                                 
   student *pstud=theclass;  //pstud 为指向student 结构的指针,其值被初始化成数组theclass的首地址, 
   
   for(int i=0;i<student__num;i++)//通过时pstud自增,使得pstud指向theclass[i],然后调用函数输出数组中各元素的成员值 
     displaystudentinfo(pstud++);
     
   getch();return 0; 
    
} 

void displaystudentinfo(const student *const stud)//const 限定防止原数组被改变 
{
       cout<<"num="<<stud->num<<"\t";
       cout<<"name="<<stud->name<<"\t"; 
       cout<<"sex="<<stud->sex<<"\t";
       cout<<"score="<<stud->score<<endl<<endl;       
     
     
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值