枚举

枚举

常量符号化

  1. 用符号而不是具体的数字来表示程序中的数字

    1
    2
    3
    const int red=0;
    //然后直接使用类型的名字
    //等等
  2. 枚举是一种用户定义的数据类型,他用关键字enum来声明

    1
    enum 枚举类型的名字{名字0······名字n};
  3. 枚举类型的名字通常不直接使用,而是使用大括号里面的名字

    1
    2
    3
    4
    5
    enum colors{red,yellow,green,numcolors};
    //大括号里面的类型是int 他们从0到n
    //因为最后一个元素可以用于计数,统计前面有多少个enum
    //如这里,numcolors就表示有3个enum变量
    enum color{red=1,yellow=5,green=3};
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    //使用举例:
    enum name {red=1,green=10,black=3};


    int main()
    {
    printf("%d",red);
    return 0;
    }

  4. 枚举常用于定义符号量,声明在main以前

结构类型

  1. strcut:声明在主函数内外都可以,但是想要结构可以在更多的函数中运用最好放在,主函数外面

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    //形式1

    struct date
    {
    int day;
    int month;
    int year;
    };
    //一定要输入分号
    int main()
    {
    struct date today;
    //可以理解为,struct定义了多个数据类型
    //这里给数据起名字为today
    today.day=244;
    //名字加数据类型
    printf("%d",today.day);
    return 0;
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    //形式二

    struct
    {
    int x;
    int y;
    }p1,p2;
    //p1和p2都是一种无名结构,里面有x,y
    //这种类型不常见,用于周期不长
    1
    2
    3
    4
    5
    6
    struct date
    {
    int x;
    int y;//(成员变量)
    }p1,p2;//变量列表

    1
    2
    3
    4
    5
    6
    struct Node
    {
    int date;
    struct point p;
    struct Node* next;
    }n1={10,{4,5},NULL};
  2. 成员初始化是0,

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    struct date
    {
    int day;
    int month;
    int year;
    };


    int main()
    {
    struct date today={27,10,2022};
    struct date chg;
    printf("%i-%i-%i\n\n",today.year,today.month,today.day);
    chg.day=27;
    chg.month=10;
    chg.year=2022;
    printf("%i-%i-%i",chg.day,chg.month,chg.year);
    return 0;
    }
  3. 结构用.或者-> 运算符和名字访问其他成员,-struct stu

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    struct stu
    {
    char name[20];
    int age;
    };

    int main()
    {
    struct stu chg={"chg",19};
    struct stu *i=&chg;
    printf("%s\n",(*i).name);
    printf("%s\n",i->name);
    printf("%s\n",chg.name);

    return 0;
    }
  4. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    struct date
    {
    int x;
    int y;
    }p1,p2;

    //访问成员的方式
    int main()
    {
    struct date
    return 0;
    }
    p1.x=p1=date.x;
    p2.y=p2=date.y;
    p1=p2--->p1.x=p2.x,p1.y=p2.y;
  5. 结构运算

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    int main()
    {
    struct date today={27,10,2022};
    struct date chg;
    printf("%i-%i-%i\n\n",today.year,today.month,today.day);
    chg.day=27;
    chg.month=10;
    chg.year=2022;
    printf("%i-%i-%i\n\n",chg.day,chg.month,chg.year);
    chg=today;
    chg.year=2021;
    printf("%i-%i-%i\n\n",chg.day,chg.month,chg.year);
    return 0;
  6. 结构内容的打印

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    struct stu
    {
    int age;
    char name[20];
    int number[20];
    //必须加数字
    };

    int main()
    {
    struct student chg={19,chg,"2022124032"};
    //字符串的创建用""
    printf("%d,%d,%s",chg.age,chg.name,chg.number);
    return 0;
    }
  7. 嵌套结构只能,在主函数定义,不能在结构体里面定义,但需要命名

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    struct time
    {
    int hour;
    int min;
    int second;
    };

    struct today
    {
    struct time now_time;//不能在这定义now_time的内容,需要定义名字
    int year;
    int month;
    int day;
    };


    int main()
    {
    struct today TIME={{23,02,34},2022,10,28};
    return 0;
    }

结构与函数

  1. &可以用取地址取到该结构体变量具体的某一类型的地址,而且可以打印出来

  2. 传结构给函数,是传值,如果有函数使用过那该值为0

  3. 用结构也可以接受函数返回的结构的值

  4. 将结构体变量传入函数的方法是:将结构体变量地址给函数,函数中用指针接受,然后用指针的用法,指需要用的东西

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    //传址调用

    struct stu
    {
    char name[20];
    int age;
    };
    void print(struct stu *chg_1)
    {

    printf("%s",chg_1->name);//chg_1相当于指针的用法
    printf("%s",(*chg_1).name);
    }

    int main()
    {
    struct stu chg={"chg",19};
    struct stu *i=&chg;
    printf("%s\n",(*i).name);
    printf("%s\n",i->name);
    printf("%s\n",chg.name);
    print(&chg);
    return 0;
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    //传值调用


    struct stu
    {
    char name[20];
    int age;
    };
    void print2(struct stu chg_2)
    {
    printf("\n%s\n",chg_2.name);
    }

    int main()
    {
    struct stu chg={"chg",19};
    struct stu *i=&chg;
    printf("%s\n",(*i).name);
    printf("%s\n",i->name);
    printf("%s\n",chg.name);
    print(&chg);
    return 0;
    }
  5. 由于传值调用需要开辟新空间,可能导致栈压力过大,所以更好的方法是传址调用

    • 栈存储数据先进后出,后进的先出

    • 函数调用的参数压栈,从下往上存储数据时叫压栈,

    • 出栈是从上往下删

函数与栈区

  1. 函数调用都会在内存的栈区上,开辟新的空间
  2. 函数传参,从右往左传参
  3. 自己写的函数也要占用空间

枚举
https://tsy244.github.io/2023/04/20/C/枚举/
Author
August Rosenberg
Posted on
April 20, 2023
Licensed under