typedef struct

来源:互联网 发布:淘宝看戏机哪个牌子好 编辑:程序博客网 时间:2024/04/28 22:36
step 1:
#exaple
struct girl
{
int age;
int weight;
int height;
struct girl *next/*特别注意结构体指针*/
};
-------->struct是申明结构体类型是所必须使用的关键字,不能省略
-------->girl为结构体名,可以省略!
-------->struct girl是一个类型名,它就和int,char,float,double差不多

通用形式是:
struct 结构体名
{成员列表};
step 2:
结构类型变量的说明
1. 先定义结构,再说明结构变量
struct stu
{
   int num;
   char name[20];
   char sex;
   float score;
};
struct stu boy1,boy2;
2.在定义结构类型的同时说明结构变量
struct stu
{
   int num;
   char name[20];
   char sex;
   float score;
}stu boy1,boy2;
3.直接说明结构变量
struct/*注意此处省略了结构体名*/
{
   int num;
   char name[20];
   char sex;
   float score;
}stu boy1,boy2;
4.成员也可以又是一个结构
struct date
{
   int month;
   int day;
   int year;
};
struct
{
   int num;
   char name[20];
   char sex;
   struct date birthday;
   float score;
}boy1,boy2;

step 3:
typedef的用法
typedef定义的一般形式为:
ypedef 原类型名   新类型名

typedef struct stu
{
   char name[20];
   int age;
   char sex;
} STU;
定义STU表示struct stu 别名,然后可用STU来说明结构变量:
STU body1,body2;
STU *next;

 
原创粉丝点击