蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测 C11 结构体初始化、值传与指针箭头访问、链表/互引用及 sizeof/offsetof 对齐填充

C本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。explore-structures.c
explore-structures.c115 行
#include <stddef.h>
#include <stdio.h>
#include <string.h>

struct Book {
    char title[50];
    char author[50];
    char subject[100];
    int book_id;
};

struct Simple {
    int a;
    char b;
    double c;
};

struct Complex {
    char label[16];
    struct Simple inner;
};

struct Node {
    int value;
    struct Node *next;
};

struct PartnerB;
struct PartnerA {
    int id;
    struct PartnerB *peer;
};
struct PartnerB {
    int id;
    struct PartnerA *peer;
};

struct Person {
    char name[20];
    int age;
    float height;
};

struct Padded {
    char a;
    int b;
    char c;
};

struct Compact {
    int b;
    char a;
    char c;
};

static void mutate_book_by_value(struct Book copy) {
    copy.book_id = 999999;
    (void)copy;
}

static void update_book_by_ptr(struct Book *ptr, int new_id) {
    ptr->book_id = new_id;
}

int main(void) {
    struct Book b0 = {"C Language", "MBSKY", "Systems", 123456};
    struct Book b1;
    strcpy(b1.title, "C Programming");
    strcpy(b1.author, "Ada");
    strcpy(b1.subject, "Core C");
    b1.book_id = 6495407;
    int c1_pass = (strcmp(b0.title, "C Language") == 0 && b0.book_id == 123456 &&
                   strcmp(b1.title, "C Programming") == 0 && b1.book_id == 6495407);

    mutate_book_by_value(b1);
    int after_val = b1.book_id;
    update_book_by_ptr(&b1, 6495700);
    int after_ptr = b1.book_id;
    int c2_pass = (after_val == 6495407 && after_ptr == 6495700);

    struct Complex cx = {"wrap", {7, 'K', 2.5}};
    struct Node n3 = {30, NULL};
    struct Node n2 = {20, &n3};
    struct Node n1 = {10, &n2};
    int list_sum = 0;
    for (struct Node *cur = &n1; cur != NULL; cur = cur->next) {
        list_sum += cur->value;
    }
    struct PartnerA pa = {1, NULL};
    struct PartnerB pb = {2, &pa};
    pa.peer = &pb;
    int mutual_ok = (pa.peer->id == 2 && pb.peer->id == 1);
    int c3_pass = (cx.inner.a == 7 && list_sum == 60 && mutual_ok);

    size_t sz_person = sizeof(struct Person);
    size_t off_age = offsetof(struct Person, age);
    size_t off_height = offsetof(struct Person, height);
    size_t sz_padded = sizeof(struct Padded);
    size_t sz_compact = sizeof(struct Compact);
    int c4_pass = (sz_person == 28 && off_age == 20 && off_height == 24 &&
                   sz_padded == 12 && sz_compact == 8);

    printf("{\"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [\n");
    printf("  {\"name\": \"struct_init_and_dot_access\", \"expected\": {\"b0_id\": 123456, \"b1_id\": 6495407}, \"actual\": {\"b0_id\": %d, \"b1_id\": %d}, \"passed\": %s},\n",
           b0.book_id, 6495407, c1_pass ? "true" : "false");
    printf("  {\"name\": \"struct_value_vs_pointer_arrow\", \"expected\": {\"after_value\": 6495407, \"after_ptr\": 6495700}, \"actual\": {\"after_value\": %d, \"after_ptr\": %d}, \"passed\": %s},\n",
           after_val, after_ptr, c2_pass ? "true" : "false");
    printf("  {\"name\": \"nested_self_ref_and_forward_decl\", \"expected\": {\"nested_a\": 7, \"list_sum\": 60, \"mutual\": true}, \"actual\": {\"nested_a\": %d, \"list_sum\": %d, \"mutual\": %s}, \"passed\": %s},\n",
           cx.inner.a, list_sum, mutual_ok ? "true" : "false", c3_pass ? "true" : "false");
    printf("  {\"name\": \"sizeof_padding_and_offsetof\", \"expected\": {\"person\": 28, \"off_age\": 20, \"off_height\": 24, \"padded\": 12, \"compact\": 8}, \"actual\": {\"person\": %zu, \"off_age\": %zu, \"off_height\": %zu, \"padded\": %zu, \"compact\": %zu}, \"passed\": %s}\n",
           sz_person, off_age, off_height, sz_padded, sz_compact, c4_pass ? "true" : "false");
    printf("]}\n");
    return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。

入口文件:projects/c-structures-lab/explore-structures.c

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-structures.c,或下载右侧课程工程包。
  2. 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC 13.3.0 C11 linux/amd64,--network none)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:C

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3263.zip

下载课程关联工程包:C

课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习