小编Nek*_*eko的帖子

取消引用指向数组的 NULL 指针在 C 中是否有效?

这种行为是否已定义?

volatile long (*volatile ptr)[1] = (void*)NULL;
volatile long v = (long) *ptr;

printf("%ld\n", v);
Run Code Online (Sandbox Code Playgroud)

它起作用是因为通过取消对数组的指针的引用,我们正在接收一个数组本身,然后该数组衰减为指向它的第一个元素的指针。

更新演示:https : //ideone.com/DqFF6T

此外,GCC 甚至将下一个代码视为常量表达式:

volatile long (*ptr2)[1] = (void*)NULL;
enum { this_is_constant_in_gcc = ((void*)ptr2 == (void*)*ptr2) };
printf("%d\n", this_is_constant_in_gcc);
Run Code Online (Sandbox Code Playgroud)

基本上,在编译时取消引用 ptr2;

c arrays null pointers dereference

0
推荐指数
2
解决办法
418
查看次数

标签 统计

arrays ×1

c ×1

dereference ×1

null ×1

pointers ×1