<i id='EzUke'><tr id='EzUke'><dt id='EzUke'><q id='EzUke'><span id='EzUke'><b id='EzUke'><form id='EzUke'><ins id='EzUke'></ins><ul id='EzUke'></ul><sub id='EzUke'></sub></form><legend id='EzUke'></legend><bdo id='EzUke'><pre id='EzUke'><center id='EzUke'></center></pre></bdo></b><th id='EzUke'></th></span></q></dt></tr></i><div id='EzUke'><tfoot id='EzUke'></tfoot><dl id='EzUke'><fieldset id='EzUke'></fieldset></dl></div>

    <tfoot id='EzUke'></tfoot>

    <legend id='EzUke'><style id='EzUke'><dir id='EzUke'><q id='EzUke'></q></dir></style></legend>

    <small id='EzUke'></small><noframes id='EzUke'>

      • <bdo id='EzUke'></bdo><ul id='EzUke'></ul>

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

      Can I use NULL as substitution for the value of 0?(我可以使用 NULL 代替 0 的值吗?)
        <tbody id='D29rA'></tbody>
          <i id='D29rA'><tr id='D29rA'><dt id='D29rA'><q id='D29rA'><span id='D29rA'><b id='D29rA'><form id='D29rA'><ins id='D29rA'></ins><ul id='D29rA'></ul><sub id='D29rA'></sub></form><legend id='D29rA'></legend><bdo id='D29rA'><pre id='D29rA'><center id='D29rA'></center></pre></bdo></b><th id='D29rA'></th></span></q></dt></tr></i><div id='D29rA'><tfoot id='D29rA'></tfoot><dl id='D29rA'><fieldset id='D29rA'></fieldset></dl></div>
        1. <small id='D29rA'></small><noframes id='D29rA'>

          • <bdo id='D29rA'></bdo><ul id='D29rA'></ul>
            <legend id='D29rA'><style id='D29rA'><dir id='D29rA'><q id='D29rA'></q></dir></style></legend>

              • <tfoot id='D29rA'></tfoot>
                本文介绍了我可以使用 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 的值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                What is a dangling reference?(什么是悬空引用?)
                C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)

                  1. <legend id='a7wnw'><style id='a7wnw'><dir id='a7wnw'><q id='a7wnw'></q></dir></style></legend>
                      <bdo id='a7wnw'></bdo><ul id='a7wnw'></ul>
                        <tbody id='a7wnw'></tbody>
                      • <i id='a7wnw'><tr id='a7wnw'><dt id='a7wnw'><q id='a7wnw'><span id='a7wnw'><b id='a7wnw'><form id='a7wnw'><ins id='a7wnw'></ins><ul id='a7wnw'></ul><sub id='a7wnw'></sub></form><legend id='a7wnw'></legend><bdo id='a7wnw'><pre id='a7wnw'><center id='a7wnw'></center></pre></bdo></b><th id='a7wnw'></th></span></q></dt></tr></i><div id='a7wnw'><tfoot id='a7wnw'></tfoot><dl id='a7wnw'><fieldset id='a7wnw'></fieldset></dl></div>

                        <tfoot id='a7wnw'></tfoot>

                        <small id='a7wnw'></small><noframes id='a7wnw'>