美文网首页
SQSH1616 12-20 C基础

SQSH1616 12-20 C基础

作者: 彳亍的心 | 来源:发表于2016-12-20 19:10 被阅读0次

    一。复杂结构类型

      结构体
      联合体
      枚举类型
    

    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));
    

    头结点标示一个链表,即链表名称
    头结点的数据域不存放数据,指针域存放第一个结点的地址,
    头结点只是为了标示这个链表

    相关文章

      网友评论

          本文标题:SQSH1616 12-20 C基础

          本文链接:https://www.haomeiwen.com/subject/azrfvttx.html