随笔-C-typdef定义数组

typdef定义数组后的初始化 | 计算机内部只知晓地址,类型为上层的高级语义

#include <stdio.h>

typedef int ARR_INT_2[2];

void test(ARR_INT_2 *t)
{
    int *t1;
    int *t2;

    t1 = &(((int*)t)[0]);
    t2 = &(((int*)t)[1]);

    printf("t1 addr - %p\n", t1);
    printf("t2 addr - %p\n", t2);

    *t1 = 3;
    *t2 = 4;
}

int main()
{
    ARR_INT_2 a;

    a[0] = 1;
    a[1] = 2;

    printf("a[0] addr - %p\n", &a[0]);
    printf("a[1] addr - %p\n", &a[1]);

    test(&a);

    printf("a[0] val - %d\n", a[0]);
    printf("a[1] val - %d\n", a[1]);

    return 0;
}

输出结果:

a[0] addr - 0x7ffc03144fd8
a[1] addr - 0x7ffc03144fdc
t1 addr - 0x7ffc03144fd8
t2 addr - 0x7ffc03144fdc
a[0] val - 3
a[1] val - 4
posted @ 2023-07-23 18:03  LiYanbin  阅读(11)  评论(0编辑  收藏  举报