//结构指针变量的声名和使用示例

本文展示了一个关于结构指针变量声明和使用的示例代码。该示例定义了一个名为'student'的结构体,并创建了一个结构实例。通过结构变量和指针两种方式访问结构成员,演示了如何使用圆点运算符和箭头运算符。
//结构指针变量的声名和使用示例 
#include<iostream.h>
#include<conio.h>

struct student
{
   int num;
   char name[20];
   char sex;
   float score;       
       
       
} ;

int main()
{
  student stud={120,"weishanshan",'N',92};
  student *pstud=&stud;
  //通过结构变量访问结构的成员
       cout<<"Access structure through structure variable:\n";
       cout<<"num="<<stud.num<<"\t";
       cout<<"name="<<stud.name<<"\t"; 
       cout<<"sex="<<stud.sex<<"\t";
       cout<<"score="<<stud.score<<endl<<endl;   
   
   //通过指针访问结构,使用圆点运算符结构的成员(*^__^*) 
       cout<<"Access structure through pointer and (.) variable:\n";
       cout<<"num="<<(*pstud).num<<"\t";
       cout<<"name="<<(*pstud).name<<"\t"; 
       cout<<"sex="<<(*pstud).sex<<"\t";
       cout<<"score="<<(*pstud).score<<endl<<endl;    
   //通过指针访问结构,使用箭头运算符     
       cout<<"Access structure through pointer and (->) variable:\n";
       cout<<"num="<<pstud->num<<"\t";
       cout<<"name="<<pstud->name<<"\t"; 
       cout<<"sex="<<pstud->sex<<"\t";
       cout<<"score="<<pstud->score<<endl<<endl;   
           
  getch();
  return 0;         
           
} 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值