C语言-5、结构体

2022-07-26  本文已影响0人  喂_balabala
写法一
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>

struct Dog {
    // 成员
    char name[10]; // copy进去
    int age;
    char sex;

}; // 必须给写分号;

int main() {
    struct Dog dog; // 这样写完,成员是没有任何初始化的,成员默认值 是系统值(name:?@, age:3133440, sex:€)
    printf("name:%s, age:%d, sex:%c \n", dog.name, dog.age, dog.sex);
    // 赋值操作
    // dog.name = "旺财";
    strcpy(dog.name, "旺财");
    dog.age = 3;
    dog.sex = 'G';
    printf("name:%s, age:%d, sex:%c \n", dog.name, dog.age, dog.sex);
    return 0;
}
写法二
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>

struct Person {
    // 成员
    char * name; // 字符指针 = "赋值"
    int age;
    char sex;
} ppp = {"Derry", 33, 'M'},
ppp2,
ppp3,
pppp4,
pppp5
// ...
;

int main() {
    // Person == ppp == struct Person ppp;
    printf("name:%s, age:%d, sex:%c \n", ppp.name, ppp.age, ppp.sex);
    // 赋值
    // strcpy(pppp5.name, "Derry5"); // Copy不进去 字符串指针可以直接赋值了
    pppp5.name = "DerryO";
    pppp5.age = 4;
    pppp5.sex = 'M';
    printf("name:%s, age:%d, sex:%c \n", pppp5.name, pppp5.age, pppp5.sex);
    return 0;
}
写法三
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>

struct Study {
    char * studyContent; // 学习的内容
};

struct Student {
    char name[10];
    int age;
    char sex;

    // Study study; // VS的写法
    struct Study study; // Clion工具的写法

    struct Wan {
        char * wanContent; // 玩的内容
    } wan;
};

int main() {
    struct Student student =
            {"李元霸", 88, 'm' ,
             {"学习C"},
             {"王者农药"}
             };
    printf("name:%s, age:%d, sex:%c,study:%s, wan:%s \n",
           student.name, student.age, student.sex, student.study.studyContent, student.wan.wanContent);
    return 0;
}
结构体指针
#include <stdio.h>
#include <string.h>

struct Cat {
    char name[10];
    int age;
};

int main() { // 栈
    // 结构体
    struct Cat cat = {"小花猫", 2};
    // 结构体 指针    -> 调用一级指针成员
    // VS的写法:Cat * catp = &cat;
    struct Cat * catp = &cat;
    catp->age = 3;
    strcpy(catp->name, "小花猫2");
    printf("name:%s, age:%d \n", catp->name, catp->age);
    return 0;
}
结构体指针 与 动态内存开辟
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Cat2 {
    char name[10];
    int age;
};

int main() { // 堆

    // VS的写法:Cat2 * cat = (Cat2 *) malloc(sizeof(Cat2));
    struct Cat2 *cat = malloc(sizeof(struct Cat2));

    strcpy(cat->name, "金色猫");
    cat->age = 5;

    printf("name:%s, age:%d \n", cat->name, cat->age);

    // 堆区的必须释放
    free(cat);
    cat = NULL;

    return 0;
}
结构体的数组
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Cat3 {
    char name[10];
    int age;
};

int main() {

    // 栈区 静态范畴
    struct Cat3 cat [10] = {
            {"小黄", 1},
            {"小白", 2},
            {"小黑", 3},
            {},
            {},
            {},
            {},
            {},
            {},
            {},
    };

    // VS的写法
    // cat[9] = {"小黑9", 9},
    // ClION的写法
    struct Cat3 cat9 =  {"小黑9", 9};
    // cat[9] = cat9;
    *(cat + 9) = cat9;
    printf("name:%s, age:%d \n", cat9.name, cat9.age);
    // 堆区 动态范畴 ==============================
    struct Cat3 * cat2 = malloc(sizeof(struct Cat3) * 10);
    // 【1元素地址的操作】给他赋值,请问是赋值,那个元素  (默认指向首元素地址)
    strcpy(cat2->name, "小花猫000");
    cat2->age = 9;
    printf("name:%s, age:%d \n", cat2->name, cat2->age);
    // 【8元素地址的操作】 给第八个元素赋值
    cat2 += 7;
    strcpy(cat2->name, "小花猫888");
    cat2->age = 88;
    printf("name:%s, age:%d \n", cat2->name, cat2->age);
    free(cat2);
    cat2 = NULL;
    return 0;
}

结构体与结构体指针 取别名
#include <stdio.h>
#include <stdlib.h>

struct Workder_ {
    char name[10];
    int age;
    char sex;
};

// VS的写法:typedef Workder_
typedef struct Workder_ Workder_; // 给结构体取别名

typedef Workder_ * Workder; // 给结构体指针取别名

// C库的源码,系统源码...,为什么 typedef 还取一个和结构体一样的名字(兼容代码的写法,保持一致)

int main() {
    // 以前 Clion工具 必须加上 struct   VS又不用加  代码差异化大
    // struct Workder_ workder1 = malloc(sizeof(struct Workder_));
    // 现在 (兼容代码的写法,保持一致)
    Workder_ workder1 = malloc(sizeof(Workder_));
    // VS  CLion  他们都是一样的写法
    Workder workder = malloc(sizeof(Workder_));
    return 0;
}
取别名 兼容写法 系统源码都是这样写的
#include <stdio.h>
#include <stdlib.h>

struct DAO {
    char name[10];
    int age;
    char sex;
};

// 匿名结构体的别名(这样写意义不大,因为没有名字)
typedef struct {
    char name[10];
    int age;
    char sex;
};

// 源码是这样写的
// 给结构体AV 取了一个别名等于AV
typedef struct {
    char name[10];
    int age;
    char sex;
} AV;

// 取一个别名
typedef struct DAO DAO;

void show(DAO dao) {} // 在不同工具上 又的要加,又的不用加 又差异化

int main() {
    // VS 不需要这样写,   Clion工具 要加入关键字  代码不统一
    // struct DAO * dao  = malloc( sizeof(struct DAO));
    // 加别名后  代码的统一了
    // VS
    DAO * dao  = malloc( sizeof(DAO));
    // CLion工具也这样写
    DAO * dao1  = malloc( sizeof(DAO));
    // xxx 工具也这样写
    DAO * dao2  = malloc( sizeof(DAO));
    // 加别名后  代码的统一了
    // C库的源码,系统源码...,为什么 typedef 还取一个和结构体一样的名字(兼容代码的写法,保持一致)
    AV av = {"VideoInfo", 54, 'M'}; // 结构体  VS  Clion  xxx工具  兼容写法
    AV * avp = malloc(sizeof(AV)); // 结构体指针
    return 0;
}
枚举
#include <stdio.h>

// 枚举 int 类型的
enum CommentType {
    TEXT = 10,
    TEXT_IMAGE,
    IMAGE
};

int main() {
    // Clion工具的写法如下:
    enum CommentType commentType = TEXT;
    enum CommentType commentType1 = TEXT_IMAGE;
    enum CommentType commentType2 = IMAGE;

    // VS工具的写法如下:
    // CommentType commentType = TEXT;

    printf("%d, %d, %d \n", commentType, commentType1, commentType2);

    return 0;
}


上一篇 下一篇

猜你喜欢

热点阅读