美文网首页
C语言-5、结构体

C语言-5、结构体

作者: 喂_balabala | 来源:发表于2022-07-26 15:36 被阅读0次
写法一
#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;
}


相关文章

  • C语言和OC的结构体(struct)

    Struct(结构体) 1.结构体定义 2.结构体变量 3.结构体数组 4.C语言结构体指针 5.C语言共用体 6...

  • C语言-5、结构体

    写法一 写法二 写法三 结构体指针 结构体指针 与 动态内存开辟 结构体的数组 结构体与结构体指针 取别名 取别名...

  • C语言结构体用法很多,坑也很多

    C语言可谓是编程界的传奇语言,历经几 十 年,依然排名前列。 本文主要说的是C语言中的结构体,结构体是C语言中重要...

  • C语言结构体

    结构体 本文介绍C语言结构体,struct 在C++中功能相对C较多,相当于类,这里暂时不讨论,本文单独讨论C语言...

  • 嵌入式学习笔记19.11.25

    c语言结构体 结构体一般定义全局变量 struct stu{//struct 定义结构体 stu 结构体名称 in...

  • 12-Go语言结构体

    结构体 结构体的基本概念 什么是结构体Go语言中的结构体几乎和C语言中的结构体一模一样都需要先定义结构体类型, 再...

  • C语言学习笔记-结构体占用内存大小的计算

    引言 结构体在C语言中虽然经常使用,但是怎么计算一个结构体占用多大的内存,很多C语言的新手都没注意过,其实C语言的...

  • 结构体与指针

    1.1 Linux C语言结构体 简介:本课程深入的讲解了C语言中,预处理是怎么回事,结构体和公用体又是如何使用及...

  • c语言中的结构体

    结构体是 C 语言主要的自定义类型方案,这篇就来认识一下结构体。 一、结构体的形态 C源程序(struct.c):...

  • 闲聊C语言结构体

    结构体是 C 语言主要的自定义类型方案,这篇就来认识一下结构体。 一、结构体的形态 C源程序(struct.c):...

网友评论

      本文标题:C语言-5、结构体

      本文链接:https://www.haomeiwen.com/subject/wimoirtx.html