结构体我们该如何来正确定义的4中方法

结构体是C语言中经常会用到的因此结构体必须要学好,不管你是做嵌入式单片机还是C语言开发,我们就一定义结构体做个开头,简单的说几个,下边会有详细的讲解 资料提供参考。

1. 先定义结构体类型,再定义结构体变量。

struct student{
    char no[20];       //学号
    char name[20];    //姓名
     char sex[5];    //性别
    int age;          //年龄
};             
struct student stu1,stu2;
//此时stu1,stu2为student结构体变量

  2. 定义结构体类型的同时定义结构体变量。

struct student{
    char no[20];        //学号
    char name[20];     //姓名
    char sex[5];      //性别
    int age;            //年龄
} stu1,stu2; 

  

此时还可以继续定义student结构体变量,如:

struct student stu3;

3、不指定类型名而直接定义结构体变量

struct{
    char no[20];        //学号
    char name[20];      //姓名
    char sex[5];      //性别
    int age;          //年龄
} stu1,stu2;  

  

一般不使用这种方法,因为直接定义结构体变量stu1、stu2之后,就不能再继续定义该类型的变量。

4、用typedef定义结构体变量

typedef struct stdudent
 
{
       char name[20];
       int age;
}student_t;

  

上面的代码,定义了一个结构体变量类型,这个类型有2个名字:第一个名字是struct student;第二个类型名字是student_t.

定义了这个之后,下面有2中方法可以定义结构体变量

第一种: struct student student_1; //定义了一个student_1的结构体变量

第二种:student_t student_1 //定义了一个student_1的结构体变量

 

- 新手必备:C畅销数据类型--结构体普及与应用 -

http://www.makeru.com.cn/live/5413_1909.html?s=45051

猜你喜欢

转载自www.cnblogs.com/QianD/p/11126004.html