C语言基础学习——第9天(结构体、联合、枚举)

00struct.c

#include <stdio.h>

// 编译器对齐, 让每个成员对齐(CPU -- RAM)访问边界.
struct s {
    int a;
    int b;
    char str[6];
};

int main(void)
{
    struct s obj = {
        .a = 123,
        .b = 88,
        .str = "hello",
    };

    printf("sizeof obj = %u\n", sizeof obj);

    // . 是最高优先级的运算符
    printf("content of obj: %d, %d, %s\n", obj.a, obj.b, obj.str);
    printf("-------------------------\n");

    printf("&obj = %p\n", &obj);
    printf("&obj.a   = %p, &obj.b   = %p\n", &obj.a, &obj.b);
    printf("obj.str  = %p, &obj.str = %p\n", obj.str, &obj.str);

    printf(" obj.str + 1 = %p\n", obj.str + 1);
    printf("&obj.str + 1 = %p\n", &obj.str + 1);    // 地址+5
    printf("=========================\n");

    struct s *pobj = NULL;
    pobj = &obj;

    printf("sizeof pobj = %u\n", sizeof pobj);

    // -> 和 . 优先级相同.
    printf("content of obj: %d, %d, %s\n", pobj->a, pobj->b, pobj->str);

    printf("-------------------------\n");
    printf("&pobj = %p, pobj = %p, &obj = %p\n", &pobj, pobj, &obj);

    printf("&pobj->a   = %p, &pobj->b   = %p\n", &pobj->a, &pobj->b);
    printf("pobj->str  = %p, &pobj->str = %p\n", pobj->str, &pobj->str);

    printf(" pobj->str + 1 = %p\n", pobj->str + 1);
    printf("&pobj->str + 1 = %p\n", &pobj->str + 1);

    return 0;
}


运行结果:
sizeof obj = 16
content of obj: 123, 88, hello
-------------------------
&obj = 0xbfb5337c
&obj.a   = 0xbfb5337c, &obj.b   = 0xbfb53380
obj.str  = 0xbfb53384, &obj.str = 0xbfb53384
 obj.str + 1 = 0xbfb53385
&obj.str + 1 = 0xbfb5338a
=========================
sizeof pobj = 4
content of obj: 123, 88, hello
-------------------------
&pobj = 0xbfb53378, pobj = 0xbfb5337c, &obj = 0xbfb5337c
&pobj->a   = 0xbfb5337c, &pobj->b   = 0xbfb53380
pobj->str  = 0xbfb53384, &pobj->str = 0xbfb53384
 pobj->str + 1 = 0xbfb53385
&pobj->str + 1 = 0xbfb5338a

01struct.c

#include <stdio.h>
#include <stdlib.h>

struct s {
    int a;
    int b;
    char str[6];
};

int main(void)
{
    struct s obj = {
        .a = 123,
        .b = 88,
        .str = "hello",
    };

    printf("obj's  content: %d %d %s\n", obj.a, obj.b, obj.str);
    printf("---------------------------\n");

    struct s obj1;
    obj1 = obj; // 结构体可以直接赋值
    printf("obj1's content: %d %d %s\n", obj1.a, obj1.b, obj1.str);
    printf("---------------------------\n");

    struct s *pobj = NULL;
    pobj = malloc(sizeof(struct s));
    if (NULL == pobj)
        return 0;

    *pobj = obj;
    printf("(*pobj)'s content: %d %d %s\n", pobj->a, pobj->b, pobj->str);
    printf("(*pobj)'s content: %d %d %s\n",(*pobj).a, (*pobj).b, (*pobj).str); // 这两种访问方式都可

    free(pobj);

    return 0;
}


运行结果:
obj's  content: 123 88 hello
---------------------------
obj1's content: 123 88 hello
---------------------------
(*pobj)'s content: 123 88 hello
(*pobj)'s content: 123 88 hello

02bitfield.c

#include <stdio.h>

// 位域. bit field.
struct s {
    int   field1:4;
    int   field2:20;
    int   field3:16;
};

int main(void)
{
    struct s obj;

    printf("sizeof obj = %u\n", sizeof obj);

    obj.field1 = 0xf;

    obj.field2 = 0xff;

    obj.field3 = 1234;

    printf("--------------------------\n");

    // 4 位, 最高位(1)扩展.
    printf("obj.field1 = %#x\n", obj.field1);

    // 20 位, 0xff 是8位, 最高位(0)扩展.
    printf("obj.field2 = %#x\n", obj.field2);
    printf("obj.field3 = %d\n", obj.field3);

    return 0;
}


运行结果:
sizeof obj = 8
--------------------------
obj.field1 = 0xffffffff
obj.field2 = 0xff
obj.field3 = 1234

03union.c

#include <stdio.h>

// 联合体/共用体
union u {
    int a;
    char ch;
    double d;
};

int main(void)
{
    printf("sizeof union u = %u\n", sizeof(union u));

    union u u;

    u.a = 65;
    printf("u.a = %d, u.ch = %c, u.d = %lf\n", u.a, u.ch, u.d);

    u.ch = 'a';
    printf("u.a = %d, u.ch = %c, u.d = %lf\n", u.a, u.ch, u.d);

    u.d = 3.14;
    printf("u.a = %d, u.ch = %c, u.d = %lf\n", u.a, u.ch, u.d);

    return 0;
}


运行结果:
sizeof union u = 8
u.a = 65, u.ch = A, u.d = -0.000000
u.a = 97, u.ch = a, u.d = -0.000000
u.a = 1374389535, u.ch = , u.d = 3.140000

04enum.c

#include <stdio.h>

// 枚举, 第一个成员默认值是0
enum color_type {
    COLOR_START,
    BLACK,
    WHITE,
    BLUE = 5,
    GREEN,
    RED = 8,
    YELLOW,
    OGRANGE,
    COLOR_END,
};

int main(void)
{
    enum color_type color;

    color = COLOR_START;
    printf("COLOR_START = %d\n", color);

    color = BLACK;
    printf("BLACK       = %d\n", color);

    color = GREEN;
    printf("GREEN       = %d\n", color);

    color = COLOR_END;
    printf("COLOR_END   = %d\n", color);

    return 0;
}


运行结果:
COLOR_START = 0
BLACK       = 1
GREEN       = 6
COLOR_END   = 11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cfl927096306

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值