我可以使用 NULL 代替 0 的值吗?

Can I use NULL as substitution for the value of 0?(我可以使用 NULL 代替 0 的值吗?)

本文介绍了我可以使用 NULL 代替 0 的值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 NULL 指针代替 0 的值吗?

或者这样做有什么问题吗?

<小时>

例如:

int i = NULL;

替代:

int i = 0;

<小时>

作为实验,我编译了以下代码:

#include int main(void){int i = NULL;printf("%d",i);返回0;}

输出:

<预><代码>0

确实它给了我这个警告,它本身是完全正确的:

警告:初始化从指针生成整数,无需强制转换 [-Wint-conversion]

但结果仍然是等价的.

<小时>
  • 我是否会因此而陷入未定义行为"?
  • 是否允许以这种方式使用 NULL?
  • 在算术表达式中使用 NULL 作为数值有什么问题吗?
  • 对于这种情况,C++ 中的结果和行为是什么?
<小时>

我已经阅读了有什么区别NULL, '' and 0 关于NULL, 0 之间的区别是什么,但我没有从那里获取简明信息,如果它是完全允许的并且使用 NULL 作为值来操作赋值和其他算术运算也是正确的.

解决方案

我可以使用 NULL 指针代替 0 的值吗?

,这样做是不安全的.NULL 是一个空指针常量,它可以具有 int 类型,但通常具有 void * 类型(在 C 中),否则不能直接分配给 int(在 C++ 中 >= 11).这两种语言都允许将指针转换为整数,但它们不提供隐式执行的此类转换(尽管某些编译器将其作为扩展提供).此外,虽然将空指针转换为整数以产生值 0 是很常见的,但标准并不保证这一点.如果您想要一个类型为 int 且值为 0 的常量,则将其拼写为 0.

<块引用>
  • 我可能会因此而进入未定义行为吗?

是的,在 NULL 扩展为 void * 类型的值或任何其他不能直接分配给 int 的值的任何实现上.该标准未定义您在此类实现上的赋值行为,因此其行为未定义.

<块引用>
  • 是否允许以这种方式使用 NULL 进行操作?

它的风格很差,它会在某些系统和某些情况下崩溃.由于您似乎在使用 GCC,如果您使用 -Werror 选项编译,它会在您自己的示例中中断.

<块引用>
  • 在算术表达式中使用 NULL 作为数值有什么问题吗?

是的.根本不保证有数值.如果你的意思是 0 那就写 0,这不仅定义明确,而且更短更清晰.

<块引用>
  • C++ 中的结果如何?

C++ 语言在转换方面比 C 更严格,并且对 NULL 有不同的规则,但实现也可能提供扩展.同样,如果您的意思是 0,那么这就是您应该写的内容.

Am I allowed to use the NULL pointer as replacement for the value of 0?

Or is there anything wrong about that doing?


Like, for example:

int i = NULL;

as replacement for:

int i = 0;


As experiment I compiled the following code:

#include <stdio.h>

int main(void)
{
    int i = NULL;
    printf("%d",i);

    return 0;
}

Output:

0

Indeed it gives me this warning, which is completely correct on its own:

warning: initialization makes integer from pointer without a cast [-Wint-conversion] 

but the result is still equivalent.


  • Am I crossing into "Undefined Behavior" with this?
  • Is it permissible to utilize NULL in this way?
  • Is there anything wrong with using NULL as a numerical value in arithmetical expressions?
  • And what is the result and behavior in C++ for this case?

I have read the answers of What is the difference between NULL, '' and 0 about what the difference between NULL, and 0 is, but I did not get the concise information from there, if it is quite permissible and also right to use NULL as value to operate with in assignments and other arithmetical operations.

解决方案

Am I allowed to use the NULL pointer as replacement for the value of 0?

No, it is not safe to do so. NULL is a null-pointer constant, which could have type int, but which more typically has type void * (in C), or otherwise is not directly assignable to an int (in C++ >= 11). Both languages allow pointers to be converted to integers, but they do not provide for such conversions to be performed implicitly (though some compilers provide that as an extension). Moreover, although it is common for converting a null pointer to an integer to yield the value 0, the standard does not guarantee that. If you want a constant with type int and value 0 then spell it 0.

  • Am I might crossing into Undefined Behavior with this?

Yes, on any implementation where NULL expands to a value with type void * or any other not directly assignable to int. The standard does not define the behavior of your assignment on such an implementation, ergo its behavior is undefined.

  • is it permissible to operate with the NULL in that way?

It is poor style, and it will break on some systems and under some circumstances. Inasmuch as you appear to be using GCC, it would break in your own example if you compiled with the -Werror option.

  • Is there anything wrong about to use NULL as numerical value in arithmetical expressions?

Yes. It is not guaranteed to have a numerical value at all. If you mean 0 then write 0, which is not only well defined, but shorter and clearer.

  • And how is the result in C++ to that case?

The C++ language is stricter about conversions than is C and has different rules for NULL, but there, too, implementations may provide extensions. Again, if you mean 0 then that's what you should write.

这篇关于我可以使用 NULL 代替 0 的值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:我可以使用 NULL 代替 0 的值吗?

基础教程推荐