SQSH1616 12-20 C基礎

一。復雜結構類型

  結構體
  聯合體
  枚舉類型

1.結構體

基本定義
struct  結構名
{
//成員列表
};//分號作為結束
成員列表:
    有基本數據類型定義的變量或者是構造類型的變量
    example:
        struct  student
        {
           int grade;
           int age;
           char name[32];
         };
 student:結構體名稱
 struct student:結構數據類型,相當于int,
                 double,char等基本數據結構;
 struct student stu;
        stu:結構體變量
 訪問結構成員:"."
     訪問結構體成員:
           stu.name;
           stu.grade;
           stu.age;

2.結構體變量的初始化

  #include<stdio.h>
  #include<string.h>      
  struct student
  {
  char name[32];
  char sex;
  int age;
  };

(1)初始化1

   struct student boy;
   strcpy(boy.name,"jeck");
   boy.age=24;
   boy.sex='m';

(2)初始化2

 struct  student stu1 = {"lily",'f',20}
 printf("name:%s\nsex:%c\nage:%d\n",stu1.name,stu1.sex,stu1.age) 
 注意:初始化時,一定要與成員一一對齊

(3)初始化3:生命結構體時,定義結構體變量

 一 struct student
   { 
     char name[32];
     char sex;
     int age ;
   }stu,stu1;

補上:

  嵌套定義結構體:
       struct student //(大小不固定)
       {
       int a;
       char b;
      //因struct student大小不確定,無法分配空間
     struct student stu;//error
       };

      struct student
     {
      int a;
      char b;
    //指針大小是固定的,可以
    struct student *ps;//(*ps大小固定)
   };              

3.無名結構體

 struct
 {
 int age;
 char name[16];
 }stu;
 無名結構體一般不使用

 #include<stdio.h>
 #include<string.h>
 struct student
 {
 int age;
 char name[32];
 };
  int main()
 {
  struct stuent stu,stu1;
 stu.age = 24;
  strcpy(stu.name,"lily";
  printf("%s\t%d\n",stu.name,stu.age);
  stu1=stu;
 printf("%s\t%d\n",stu1.name,stu1.age);
return 0;
}

4.宏定義結構替

 struct student
   { 
     char name[32]
     char sex;
     int age ;
   }; 
  #define STU struct student
   STU stu,stu1;<------> st 

5.結構替的嵌套

struct date
{
  int year;
  int month;
  int day;
  };
 struct student
 {
 char name[32];
 int age ;
 struct date birthday;
 };    

6.結構體數組 struct_arr.c

#include<stdio.h>
#include<string.h>
struct student
{
int age
char name[32];
};
int main()
{

//結構替數組初始化
//struct student stu,stu1,stu2;
struct student arr[3]=
{
{24,"hello"},
{20,"lily"},
{26,"jack"}
};
//結構替訪問
printf("arr[1].age=%d\narr[1].name%s\n",arr[1].age,arr[1].name);
return 0;

7.結構體指針

malloc(); //申請堆空間
free(); //釋放空間
//申請一塊堆空間,大小為:sizeof(struct date)
pa= (struct date *)malloc(sizeof(struct date))
free(pa); //釋放申請的堆空間

8.typedef

重新取名
typedef int I
即給int取名為 I;
結構體
typedef struct student
{
int age;
char name[32];
}STU;
STU stu;------>struct student stu;
和宏定義的區別:

   typedef struct student STU
   #define STU struct student     

9.結構體大小

內存對齊:
Linux: 4字節
Windows:8字節
默認從偏移量為0的位置開始存儲
每個成員所占字節是其自身大小的整數倍.
int [4];short[2];long[8]

10.聯合體

 union untype
 {
 int a ;
 long b;
 int arr[4];
 };

特點:
每次只能操作一個成員變量!!!
分配空間;
按最大數據類型分配空間

11.枚舉類型

 enum entype
 {
 A,
 b,
 c,
 }    

12.鏈表

鏈式存儲結構,線性存儲結構
其大小可動態改變,鏈表是由一個個結點串起來的數據鏈
結點:
由數據域和指針域組成
數據域:存放數據
指針域:存放下一個結點的地址

(1)創建鏈表

    struct student
    {
    int id;
    struct student *next;
    };
   
 struct student *head;
 malloc()
 free()
 創建一個頭結點:
      struct student *head;
      head = (struct student *)malloc(sizeof(struct student));

頭結點標示一個鏈表,即鏈表名稱
頭結點的數據域不存放數據,指針域存放第一個結點的地址,
頭結點只是為了標示這個鏈表

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容