• 【笔记】【C语言】第九章 用户自己建立数据类型


    9.1 定义和使用结构体变量

    9.2 使用结构体数组

    9.3 结构体指针

    9.4 用指针处理链表

    9.5 共用体类型

    9.6 使用枚举类型

    9.7 用typedef声明新类型名

    9.1 定义和使用结构体变量

    (1)

      结构体:用户自己建立的,由不同类型数据组成的组合型的,数据结构。

      结构体是派生的数据类型,使用其他类型的对象来构造结构体。

    (2)声明:

      建立结构体类型,其中并无具体数据,系统也不对他分配存储空间。

    strcut 结构体名 
    {
        成员列表
    };

    示例:

    strcut Student
    {
        int num;
        char name[20];
        char sex;
        int age;
        float score;
        char address[30];
    };
    View Code

    (3)定义:

      先声明结构体类型,再定义该类型变量。

    struct node a,b;

    (4)初始化:

      ①给全员赋初值:

    struct node
    {
        int num;
        char name[20];
        struct date
        {
            int year,month,day;
        }birthday;
        float score;
    }student = { 101, "WH" ,1982,5,21,80 };

      ②部分成员赋初值

    }student = { 101, "WH" };

    (5)引用:

      结构体成员运算符:.

      结构体指针运算符:->

    student.name
    student -> name  //等价于 (*student).name 

    (6)注意事项:

    • 一个结构体的成员,可以属于另一个结构体类型。
    • 也可以不指定结构体名,而直接定义结构体类型变量。
      • 一般形式为: struct {成员列表} 变量名列表;
    • 结构体类型 ≠ 结构体变量
      • 只能对变量赋值、存取和运算,不能对类型。
      • 类型不分配空间,只对变量分配空间。
    • 结构体类型中的成员名,可以与程序中的变量名相同,但二者代表的对象不同。
    • 域:结构体变量中的成员。

    9.2 使用结构体数组

    (1)一般形式:

    struct 结构体名 
    {
        成员列表
    } 数组名[数组长度]; 
    
    结构体类型 数组名[数组长度]; 

     9.3 结构体指针

    (1)为了方便和直观,C中允许将(*p).num 用 p->num来替代

    struct node
    {
        int num; 
    };
    struct node student; 
    struct node *p;
    //以下三个表达式相同 
    student.num 
    (*p).num
    p -> num 

    (2)用 结构体变量 和 结构体变量的指针 作函数参数

      作用:将一个结构体变量的值 传递给 另一个函数。

      方法:

    1. 用 结构体变量 的成员 作参数。
    2. 用 结构体变量 作实参。 //比较占空间
    3. 用 指向结构体变量的指针 作实参。

    (3)例如:

    #include<stdio.h>
    
    struct node
    {
        int num;
        char name[20];
        float score[3];
        float ave;
    };
    
    int main
    {
        void input( struct node stu[] );
        struct node max( struct node stu[] );
        void print( struct node stu[] );
        
        struct node stu[3];
        struct node *p = stu;
        
        input(p);
        print(max(p));
        
        return 0;
    }

     (指针部分,暂时不深入学习了)

    9.4 用指针处理链表

    (1)什么是链表:

      链表:动态的进行存储分配的一种结构

      必须使用指针变量才能实现

    (2)建立简单的静态链表

    #include<stdio.h>
    
    int main()
    {
        struct node
        {
            float score;
            struct node *next;
        }a,b,c;
        struct node *head = &a;
        
        a.next = &b ;
        b.next = &c ;
        
        return 0;
    } 

    (3)输出各节点的数据

        struct node *p = head;
        do
        {
            printf("%d\n",p.score);
            p = p -> next;
        }while( p != NULL )

    (4)建立动态链表 (不考)

      在程序执行过程中,从无到有地建立起一个链表。

      例:写一个函数,建立一个有3个学生数据地单向动态链表。

    #include<stdio.h>
    #include<stdlib.h>
    #define N sizeof(struct student)
    
    struct student
    {
        long sum;
        float score;
        struct student *next;
    };
    
    int n;
    struct student *create()
    {
        struct student *head,*p1,*p2;
        n=0;
        p1=p2= (struct student *) malloc(LEN);
        scanf("%ld%f", &p1->num , &p1->score );
        
        head=NULL;
        while(p1 -> num !=0 )
        {
            n++;
            if(n==1 ) head=p1;
            else  p2 -> next = p1;
            p2=p1;
            p1 = (struct student *) malloc (LEN);
            scanf("%ld%f", &p1->num , &p1->score );
        }
        p2 -> next = NULL;
        return head;
    }
    
    int main()
    {
        struct student *pt;
        pt= create();
        printf("\nnum:%ld\nscore:%5.1f\n", pt -> num , pt -> score);
        return 0;
    } 
    View Code

    9.5 共用体类型

    (1)什么是共用体:  

      使几个不同的变量,共享同一段内存的结构。

    union 共用体名
    { 
        成员表列 
    }变量表列 

      共用体变量所占的内存长度,等于最长的成员长度

      结构体变量 各成员占的内存长度之和。

    (2)引用共用体变量的方式

      不能引用共用体变量,只能引用共用体变量的成员

      在每一个瞬间,只能存放其中一个成员。

      其中起作用的成员,是最后一次被赋值的成员。

      共用体变量的地址和它的各成员的地址,都是同一地址。

      C89不允许共用体变量作为函数参数。

    (3)共用体数据类型的特点

      共用体类型 和 结构体类型 在定义中可以互相出现。

    9.6 使用枚举类型

    (1)枚举类型:

      把可能的值一一列举出来,变量的值只限于列举出来的值的范围。

    9.7 用typedef声明新类型名 

  • 相关阅读:
    php项目目录显示
    初识fastadmin
    php环境变量
    tp5数据库——时间查询
    tp5数据库——聚合查询
    tp5数据库——链式操作
    tp5数据库——查询语法
    MongoDB踩坑记录
    RxJS合并操作符:concat、merge、forkJoin、zip、 combineLatest 、concatAll、mergeAll、switchAll
    Git Flow
  • 原文地址:https://www.cnblogs.com/xwww666666/p/16277013.html
Copyright © 2020-2023  润新知