10枚举,结构,联合 枚举 结构类型

前言:好吧,字符串这章这些函数估计后面又要重新熟悉,毕竟函数有点多。

返回主页:https://www.cnblogs.com/accolade/p/12289618.html

枚举,结构,联合

10枚举,结构,联合
枚举
结构类型

 枚举使用是必需带上enum 枚举类型的名字 名字,怎么感觉有点烦呢。

随便写的代码

#include<stdio.h>
#include<stdbool.h>
#include<string.h>
enum color{red,green,yellow,white,colornum};
int iamyoudada(enum color c);
int main(int argc,const char *argv[]){
    
    char *colorchar[]={"red","green","yellow","white"};
    for(int i=0;i <= colornum;i++){
        printf("%d:red=%d
",i,red);
    }

    int Nm;
    enum color b = 3;
    Nm=iamyoudada(b);
    printf("the result of returnning form funtion iamyoudada:%d
",Nm);
    printf("%d
",red);
}
int iamyoudada(enum color c){
    printf("here is the funtion:iamyoudada and the input info is %d
",c);
    return 1;
}

注意:

enum color{red,green,yellow,white,colornum};
int iamyoudada(enum color c);

申明函数是,这个枚举必需也要写在外面,好奇怪。不然就会有报错。改了1个小时。

看下老师的代码:

10枚举,结构,联合
枚举
结构类型

 10枚举,结构,联合
枚举
结构类型

 上面的代码只能这么写,我是过直接去改原始enum就会报错。

由于枚举就是const int,所以函数直接可以穿参数进去。看下面的代码

#include<stdio.h>
#include<stdbool.h>
#include<string.h>

int f(int a);
int main(int argc,const char *argv[]){
    enum color { red, green, white};

    f(red);
}
int f(int a){
    printf("i am the funtion :f
a=%d
",a);
}
i am the funtion :f
a=0

枚举的小套路

10枚举,结构,联合
枚举
结构类型

 有时间自己写一下。

结构类型

10枚举,结构,联合
枚举
结构类型

 10枚举,结构,联合
枚举
结构类型

 是不是很像python里面的面向对象,但是没有可以调用的函数。所有还是遗憾的。

10枚举,结构,联合
枚举
结构类型

有趣的现象,如果int没有声明的话,便会变成了默认为0,请看下面的代码:

10枚举,结构,联合
枚举
结构类型

 结构运算

10枚举,结构,联合
枚举
结构类型

 但是如果是赋值的话,全部都赋值过来了

10枚举,结构,联合
枚举
结构类型

 指针的结构

10枚举,结构,联合
枚举
结构类型

 10枚举,结构,联合
枚举
结构类型

 输出:

10枚举,结构,联合
枚举
结构类型

注意,函数如果传的如果是值,那么修改不会修改到外面结构的属性,如下:

10枚举,结构,联合
枚举
结构类型

 10枚举,结构,联合
枚举
结构类型

 解决方案

10枚举,结构,联合
枚举
结构类型

 当然,这种方法如果结构太大的话,就不可取了。所以我们推荐下面的一种方案。指针传参,先看下下面这句话,翻译一下

10枚举,结构,联合
枚举
结构类型

 注意:在定义函数是,结构体一定要写在函数上面,因为我感觉这是从上面玩下面开始编译,如果你没有定义结构,就会报错。开始没注意,一直报错。调了好久就发现这个问题了。

请看代码:

#include<stdio.h>
#include<string.h>

struct person{
        char name[200];
        int age;
        char sex[100];
    };
void outputinfoname(struct person a);// print struct person's info,such as name,age,sex
void outputinfo(struct person *a);//change person info

int main(int argc ,char const *argv[]){
    
    struct person xiaoming;
    xiaoming.age = 15;
    strcpy(xiaoming.name,"xiaoming");
    strcpy(xiaoming.sex,"male");
    outputinfoname(xiaoming);
    struct person *xiaoming1 = &xiaoming;
    outputinfo(xiaoming1);//pass a argument of pointer
    outputinfoname(xiaoming);

    return 0;
}
void outputinfo(struct person *a){
   strcpy(a->name,"xiaoming111");
   strcpy(a->sex,"female");
   a->age = 22;
 
}
void outputinfoname(struct person a){
    printf("%s.name=%s
",a.name,a.name);
    printf("%s.sex=%s
",a.name,a.sex);
    printf("%s.age=%d
",a.name,a.age);
} 

输出结果:

xiaoming.name=xiaoming
xiaoming.sex=male
xiaoming.age=15
xiaoming111.name=xiaoming111
xiaoming111.sex=female
xiaoming111.age=22

 指向结构的指针

10枚举,结构,联合
枚举
结构类型

 结构数组与结构中的结构

10枚举,结构,联合
枚举
结构类型

 10枚举,结构,联合
枚举
结构类型

 这个可以拿去做线性表(链式结构),二叉树,中序遍历等等,后面有时间可以试一下。

10枚举,结构,联合
枚举
结构类型

 10枚举,结构,联合
枚举
结构类型

 10枚举,结构,联合
枚举
结构类型

 typeof起别名

10枚举,结构,联合
枚举
结构类型

 10枚举,结构,联合
枚举
结构类型

 10枚举,结构,联合
枚举
结构类型

由于前面我已经学习过了,所以就全部贴图了,但是注意的是可以同时起两个别名,一般有一个表示指针。 

例如把上面的代码改成这样:

#include<stdio.h>
#include<string.h>

typedef struct person{
        char name[200];
        int age;
        char sex[100];
    } xiaomingone ,*xiaomingtwo;
void outputinfoname(struct person a);// print struct person's info,such as name,age,sex
void outputinfo(struct person *a);//change person info

int main(int argc ,char const *argv[]){
    xiaomingone xiaoming;  //typedef of othername
    xiaoming.age = 15;
    strcpy(xiaoming.name,"xiaoming");
    strcpy(xiaoming.sex,"male");
    outputinfoname(xiaoming);
    xiaomingtwo xiaoming1 = &xiaoming;//typeded of pointer 
    outputinfo(xiaoming1);//pass a argument of pointer
    outputinfoname(xiaoming);

    return 0;
}
void outputinfo(struct person *a){
   strcpy(a->name,"xiaoming111");
   strcpy(a->sex,"female");
   a->age = 22;
 
}
void outputinfoname(struct person a){
    printf("%s.name=%s
",a.name,a.name);
    printf("%s.sex=%s
",a.name,a.sex);
    printf("%s.age=%d
",a.name,a.age);
}

在调用是,不用加上*了,相当于直接给个地址呢然后你再把这个地址填上你需要的指针

联合

10枚举,结构,联合
枚举
结构类型

10枚举,结构,联合
枚举
结构类型

就是说其实里面只有一个共享的空间,虽然以我现在的水平不知道这个有什么作用,但是我们可以知道我们inter CPU是基于X86结构,也就是基于小端储存的,请看代码

#include<stdio.h>
#include<string.h>



int main(int argc ,char const *argv[]){
    union person{
        int age;
        char name[sizeof(int)];
    } xiaofang;
    printf("sizeof(int)=%d
",sizeof(int));
    xiaofang.age = 1234;
    for(int i=0;i < sizeof(int);i++){
        printf("%02hhx ",xiaofang.name[i]);
    };

    return 0;
}

输出的结果:

sizeof(int)=4
ffd2 04 00 00

老师的讲解:

10枚举,结构,联合
枚举
结构类型

老师的代码:

10枚举,结构,联合
枚举
结构类型

 也说明小端结果也是以字节为单位储存的。