gpt4 book ai didi

c - void *p = 0L 有效吗?

转载 作者:行者123 更新时间:2023-12-04 10:07:55 25 4
gpt4 key购买 nike

this answer , sassman初始化一个指针:

zend_class_entry* ce = 0L;

我的问题是 – 这有效吗?我会说它不是,用空指针初始化变量或者是一个未经修饰的(并且可能被强制转换为 void *) 0 常量,或者应该使用一些计算结果为 NULL 的宏。

但是,我在支持这种解释的标准中找不到明确的语言。它只说:

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.

最佳答案

0L 是一个整数常量表达式,值为零。当用作指针时,这样的常量表达式是一个空指针常量。这正是你引用的句子所说的 (C99 6.3.2.3/3):

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.

紧接着是空指针常量可以转换为空指针的声明:

If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

为什么您认为 0 不能用作空指针常量?

关于c - void *p = 0L 有效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4527081/

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