歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C語言結構體類型定義

C語言結構體類型定義

日期:2017/3/1 9:12:16   编辑:Linux編程

C語言結構體類型定義

結構體的定義形式如下:

struct 結構體名

{

  結構體成員

};

結構體變量的定義方式有三種:
1、先定義結構體,再定義變量:

eg.

struct student{

  char name[10];

  int age;

  int student_number;

};

struct student s1,s2;

2、定義結構體的同時定義變量:
eg.

struct student{

  char name[10];

  int age;

  int student_number;

}s1,s2;

在定義結構體student的同時定義了結構體變量s1,s2.

3、只定義結構體變量

eg.

struct{

  char name[10];

  int age;

  int student_number;

}s1,s2;

在這種情況下,如果還想定義一個變量s3,那麼要使用和定義s1、s2一樣的方法。

將typedef和結構體結合,比如說:

typedef struct _student{

  char name[10];

  int age;

  int student_number;

}student;

這個時候student就不是一個變量了,它是結構體struct _student的別名,如果想定義一個變量,就可以直接使用student

student s1;

而不需要struct _student s1;

另外還可以定義結構體指針類型:

typedef struct _student{

  char name[10];

  int age;

  int student_number;

}*student;

這個時候student s1;定義的變量就是一個結構體指針s1了。等價於struct _student *s1。

Copyright © Linux教程網 All Rights Reserved