我可以使用NULL代替0值吗? [英] Can I use NULL as substitution for the value of 0?

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

问题描述

我可以使用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?

例如,

int i = NULL;

代替:

int i = 0;


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


As experiment I compiled the following code:

#include <stdio.h>

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

    return 0;
}

输出:

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] 

但结果仍然相同.

  • 我会因此而陷入不确定行为"吗?
  • 是否可以通过这种方式使用NULL?
  • 在计算表达式中使用NULL作为数字值有什么问题吗?
  • 在这种情况下,C ++的结果和行为是什么?
  • 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?

我已阅读的答案NULL,'\ 0'和0 表示NULL\00之间的区别是什么,但是我没有从那里得到简洁的信息,如果它是完全可以允许的并且也是正确的.使用NULL作为值在赋值和其他算术运算中使用.

I have read the answers of What is the difference between NULL, '\0' and 0 about what the difference between NULL, \0 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.

推荐答案

我允许使用NULL指针代替0值吗?

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

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

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

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.

  • 是否可以通过这种方式使用NULL?
  • is it permissible to operate with the NULL in that way?

它的样式很差,在某些系统上和某些情况下会损坏.由于您似乎正在使用GCC,因此如果使用-Werror选项进行编译,它将在您自己的示例中中断.

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.

  • 在运算表达式中使用NULL作为数值有什么错误吗?
  • Is there anything wrong about to use NULL as numerical value in arithmetical expressions?

是的.完全不能保证有一个数值.如果您的意思是0,请写下0,这不仅定义明确,而且更短,更清晰.

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.

  • 在这种情况下,C ++的结果如何?
  • And how is the result in C++ to that case?

C ++语言在转换方面比C语言更为严格,并且对NULL具有不同的规则,但是实现也可以提供扩展.再说一次,如果你的意思是0,那你应该写.

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值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆