zoukankan      html  css  js  c++  java
  • 2020.10.29 16课 结构体,联合,枚举

    1结构体的使用

    struct node{
        char sex;
        char age;
    }
    
    struct student {
    
    int id;
    
    char name[20];
    
    struct node mynode;
    
    }
    int main{
    struct student stu1={1,"zhangwei",'c',4};
        printf("%d	%s	%c	%d
    ",stu1.id,stu1.name,stu1.mynode.sex
              ,mynode.age);
        return 0;
    }
    

    typedef 关键字的使用 作用是为类型取别名。

    typedef struct student STU;

    typedef struct student{iint id ; char name[20];}STU;

    结构体指针用->访问成员, 结构体变量用.访问成员

    (*pstu).成员

    2.结构体大小计算

    结构体大小的计算会按照这个结构体里面的定义的最大类型来内存对齐,但是这样会浪费空间。

    double 8个 只要不超过8个 就行,超过换行

    结构体中有数组,这个数组把他看成【数组大小】个【类型】变量
    结构体中有别的结构体变量,会把比别的结构体变量展开、

    3.结构体数组

    typedef struct student{
        int id ;
        char name[20];
    }STU;
    
    STU stu[3]={{1."zhangfei"},{2."lifei"},{3."bufei"}};
    for (int i=0;i<3;i++){
        
        if(stu[i].id==2)//找到id为2的,把他的名字输出
        printf("%d	%s
    ",stu[i].id,stu[i].name);
    }
    

    3.5 typedef

    #include <stdio.h>
    
    struct node
    {
    	char sex;
    	int age;
    };
    struct student
    {
    	int id;
    	char name[20];
    	struct node mynode;
    };
    //typedef 关键字的使用
    //作用:是为类型取别名
    //typedef 类型的真名  类型的别名;
    typedef struct student stu;
    //stu是struct student的别名,可以用stu代替
    
    typedef struct stud
    {
    	int a;
    	double b;
    }stud,*pstud;
    //这个stud是这个结构体的别名,pstud是这个结构体类型指针的别名
    
    int main()
    {
    	stu stu1 = { 1,"zhangwei",'c',4 };
    
    	stud* pstud pstu = &stu1;//结构体指针指向结构体变量
    
    	结构体指针用->访问成员,结构体变量用.访问成员
    	pstu->a=10;
    	(*pstu).a;
    
    	printf("%d	%s	%c	%d
    ", stu1.id, stu1.name, stu1.mynode.sex, stu1.mynode.age);
    	getchar();
    	getchar();
    	return 0;
    }
    

    1.typedef 类型的真名 类型的别名;
    typedef struct student stu;
    //stu是struct student的别名,可以用stu代替

    2.结构体指针用->访问成员,结构体变量用.访问成员

    c

    4.共用体

    union(共用体,联合)

    联合union是一个能在同一个存储空间存储不同类型数据的类型。

    联合体所占的内存长度等于其最长成员的长度,也有叫做共用体。

    联合体虽然可以有多个成员,但同一时间只能存放其中一种。

    共用一个地址

    union data{
        int a;
        double b;
    };
    
    union data d;
    d.a=10;
    d.b=445.6
        
       
    

    由于共用体中的每个成员共用同一段内存空间,大的那个类型的空间,所以在你使用的时候,在同一时刻就能保存一个最后赋值成员的值

    5.枚举:穷举的意思就是把所有的可能全部列出来

    常用于空地围墙

    enum WeekDay{ sun, mon, tuem,wed = 45, thu, fri, sat };
    
    int main() {enum WeekDay day;
    		
    		while (1) {
    			scanf("%d", &day);
    			switch (day)
    			{
    			case sun:
    				printf("111111
    ");
    				break;
    			case mon:printf("222222
    ");
    				break;
    			case tuem:printf("333333
    ");
    				break;
    			case wed:printf("44444
    ");
    				break;
    			case thu:printf("555555
    ");
    				break;
    			case fri:printf("6666666
    ");
    				break;
    			case sat:printf("77777777
    ");
    				break;
    			default:printf("111111
    ");
    				break;
    			}
    		}
    
    	
    	
    
    	getch();
    	return 0;
    }
    
  • 相关阅读:
    springmvc
    POJ 3683 Priest John's Busiest Day
    POJ 3678 Katu Puzzle
    HDU 1815 Building roads
    CDOJ UESTC 1220 The Battle of Guandu
    HDU 3715 Go Deeper
    HDU 3622 Bomb Game
    POJ 3207 Ikki's Story IV
    POJ 3648 Wedding
    HDU 1814 Peaceful Commission
  • 原文地址:https://www.cnblogs.com/heerha/p/14040503.html
Copyright © 2011-2022 走看看