gpt4 book ai didi

c - 为什么 gcc 中允许空指针运算?

转载 作者:行者123 更新时间:2023-12-01 01:39:12 29 4
gpt4 key购买 nike

以下代码使用 gcc 编译,尽管 void ptr算术不是标准的:

int main(){
int a = 5;
void* b = (void*) &a;
b++;
}

最佳答案

这是 GCC 支持的扩展。它对待 void *char * .

来自 gcc docs :

6.24 Arithmetic on void- and Function-Pointers

In GNU C, addition and subtraction operations are supported on pointers to void and on pointers to functions. This is done by treating the size of a void or of a function as 1.

A consequence of this is that sizeof is also allowed on void and on function types, and returns 1.

关于c - 为什么 gcc 中允许空指针运算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60103420/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com