C语言结构

结构

把不同的数据类型写在一起,封装成一个大数据类型;结构的大小固定;结构中的数据都有名字
例如:
    定义新结构
    /*
        注意fish只是结构的名字
    */
    struct fish {
        /*
            const char * 是用来保存不想修改的字符串,也就是字符串的字面值
        */
        const char *name;
        const char *species;
        int teeth;
        int age;
    };

    创建数据
    struct fish snappy = {"snappy", "piranha", 69, 4}

    复制结构内容
    struct fish snappy1 = snappy;

读取结构内容

尽管结构可以向数组那样在结构中保存字段,但读取时只能按名访问;可以使用"."运算符访问结构字段
fish.c的文件:
#include <stdio.h>

int main()
{
    struct fish {
        /*
            const char * 是用来保存不想修改的字符串,也就是字符串的字面值
        */
        const char *name;
        const char *species;
        int teeth;
        int age;
    };

    struct fish snappy = {
        /*
            注意依然不能使用单引号
        */
        "snappy",
        "piranha",
        69,
        4
    };

    /*
        获取的时候是定义的结构数据的名字,而不是结构本身的名字(是snappy而不是fish)
    */
    printf("%s,%s,%i,%i", snappy.name, snappy.species, snappy.teeth, snappy.age);

    return 0;
}

使用make编译:make fish && ./fish

存储器中的结构

在定义结构时,并没有让计算机在存储器中创建任何东西,只是给了计算机一个模板,告诉它你希望新的数据类型长什么样子
当定义新变量时,计算机则需要在存储器中为结构的实例创建空间,这块空间必须足够大,以装下结构中的所有字段
把一个结构变量赋给另一个结构变量时,计算机会创建一个全新的结构副本,也就是说,计算机需要再分配一块存储器空间,大小和原来相同,然后把每个字段都复制过去

结构的嵌套

#include <stdio.h>

int main()
{
    /*
        注意结束的分号不要忘记打,这个不是数组
    */
    struct preferences {
        /*
            此处结尾是分号,不是逗号
        */
        const char *food;
        float exercise_hours;
    };

    struct fish {
        const char *name;
        const char *species;
        int teeth;
        int age;
        /*
            结构的嵌套:在结构中定义结构
            此处定义的不是一个结构体,而是一个结构创建后的数据(是struct preferences care而不是struct preferences)
        */
        struct preferences care;
    };

    struct fish snappy = {
        "snappy",
        "piranha",
        69,
        4,
        {
            "fish",
            4.00
        }
    };

    /*
        获取嵌套的结构中的数据注意care是嵌套的结构的名字
    */
    printf("%s,%f", snappy.care.food, snappy.care.exercise_hours);
}

创建别名

使用typedef将struct name重命名rename,在后续创建结构数据的时候,可以直接使用rename
其实struct name是结构名,rename是类型名
typedef struct name {} rename;

例子:
#include <stdio.h>

int main()
{
    typedef struct big_fish {
        /*
            const char * 是用来保存不想修改的字符串,也就是字符串的字面值
        */
        const char *name;
        const char *species;
        int teeth;
        int age;
    } fish;

    fish snappy = {
        /*
            注意依然不能使用单引号
        */
        "snappy",
        "piranha",
        69,
        4
    };

    printf("%s,%s,%i,%i", snappy.name, snappy.species, snappy.teeth, snappy.age);
    return 0;
}

匿名结构

typeof struct {} rename
不存在name(结构名)只存在rename(类型名)

更新结构

#include <stdio.h>

typedef struct {
    const char *name;
    const char *species;
    int age;
} turtle;

void happy_birthday(turtle t)
{
    /*
        关键在此处更新结构
    */
    t.age = t.age + 1;
    printf("happy birthday %s,you are %i years old!\n", t.name, t.age);
}

int main()
{
    turtle myrtle = {"Myrtle", "Leatherback sea turtle", 99};
    happy_birthday(myrtle);
    printf("%s's age is now %i\n", myrtle.name, myrtle.age);

    return 0;
}

为什么最终输出来的值是:
    happy birthday Myrtle,you are 100 years old!
    Myrtle's age is now 99

为什么传给函数happy_birthday的时候age递增1变成100,调用完之后再次查看age的值结果是99?
因为你传入的是结构的值,不是结构的指针,所以他会对结构的值进行复制

指针更新结构

#include <stdio.h>

typedef struct {
    const char *name;
    const char *species;
    int age;
} turtle;

void happy_birthday(turtle *t)
{
    /*
        (*t)表示myrtle指针指向的值,一定要打括号,表示一个整体
        (*t).age表示的是t指向的结构的指针,最后得到的是年龄
        *t.age表示的是t.age这个存储器单元中的内容
    */
    (*t).age = (*t).age + 1;
    printf("happy birthday %s,you are %i years old!\n", (*t).name, (*t).age);
}

int main()
{
    turtle myrtle = {"Myrtle", "Leatherback sea turtle", 99};
    /*
        注意此处传入的是结构的指针,使用'&'获取指针
    */
    happy_birthday((&myrtle));
    printf("%s's age is now %i\n", myrtle.name, myrtle.age);

    return 0;
}

易读的表达方式

#include <stdio.h>

typedef struct {
    const char *name;
    const char *species;
    int age;
} turtle;

void happy_birthday(turtle *t)
{
    /*
        (*t).age == t->age
    */
    t->age = t->age + 1;
    printf("happy birthday %s,you are %i years old!\n", (*t).name, (*t).age);
}

int main()
{
    turtle myrtle = {"Myrtle", "Leatherback sea turtle", 99};
    happy_birthday((&myrtle));
    printf("%s's age is now %i\n", myrtle.name, myrtle.age);

    return 0;
}6
坚持原创技术分享,您的支持将鼓励我继续创作!