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

实测 C11 取址解引用、NULL 判空、指针步长与二级指针及安全返回静态指针

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

static void add_by_pointer(int *target, int delta) {
    *target += delta;
}

static int *get_static_counter(void) {
    static int counter = 100;
    return &counter;
}

int main(void) {
    int var = 20;
    int *ip = &var;
    int addr_match = (ip == &var);
    int before = *ip;
    *ip = 35;
    int c1_pass = (addr_match && before == 20 && var == 35);

    int *ptr = NULL;
    int is_null = (ptr == NULL);
    int truth_if = ptr ? 1 : 0;
    int truth_not = !ptr ? 1 : 0;
    int same_ptr_sz = (sizeof(int *) == 8 && sizeof(double *) == 8 && sizeof(char *) == 8);
    int c2_pass = (is_null && truth_if == 0 && truth_not == 1 && same_ptr_sz);

    int arr[4] = {10, 20, 30, 40};
    int *p = arr;
    int third = *(p + 2);
    ptrdiff_t step = (char *)(p + 1) - (char *)p;
    const char *names[3] = {"alpha", "beta", "gamma"};
    int **pp = &ip;
    int double_deref = **pp;
    int c3_pass = (third == 30 && step == 4 && strcmp(names[1], "beta") == 0 && double_deref == 35);

    int val = 10;
    add_by_pointer(&val, 15);
    int *sp = get_static_counter();
    *sp += 23;
    int c4_pass = (val == 25 && *get_static_counter() == 123);

    printf("{\"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [\n");
    printf("  {\"name\": \"address_of_and_dereference\", \"expected\": {\"addr_match\": true, \"before\": 20, \"after\": 35}, \"actual\": {\"addr_match\": %s, \"before\": %d, \"after\": %d}, \"passed\": %s},\n",
           addr_match ? "true" : "false", before, var, c1_pass ? "true" : "false");
    printf("  {\"name\": \"null_pointer_check\", \"expected\": {\"is_null\": true, \"if_ptr\": 0, \"not_ptr\": 1, \"ptr_bytes\": 8}, \"actual\": {\"is_null\": %s, \"if_ptr\": %d, \"not_ptr\": %d, \"ptr_bytes\": %zu}, \"passed\": %s},\n",
           is_null ? "true" : "false", truth_if, truth_not, sizeof(int *), c2_pass ? "true" : "false");
    printf("  {\"name\": \"pointer_arithmetic_and_array_of_ptrs\", \"expected\": {\"third\": 30, \"step\": 4, \"name1\": \"beta\", \"pp\": 35}, \"actual\": {\"third\": %d, \"step\": %td, \"name1\": \"%s\", \"pp\": %d}, \"passed\": %s},\n",
           third, step, names[1], double_deref, c3_pass ? "true" : "false");
    printf("  {\"name\": \"pass_and_return_pointer\", \"expected\": {\"val\": 25, \"static_counter\": 123}, \"actual\": {\"val\": %d, \"static_counter\": %d}, \"passed\": %s}\n",
           val, *get_static_counter(), 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-pointers-lab/explore-pointers.c

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

下载课程关联工程包:C

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

下载课程关联工程包:C

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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