Is NULL the same as False in C++

前端 未结 4 860
你的背包
你的背包 2021-01-12 12:03

In C++ (gcc,VS) is NULL considered the same as False. Or more importantly if in a logical statement what does NULL evaluate to. There were a number of other questions but no

4条回答
  •  别那么骄傲
    2021-01-12 12:16

    It seems that you're considering the following code:

    for (;;) { }
    

    That will loop forever because the condition term is empty. You might be tempted to refer to that as the null condition to indicate that there is no condition present, but as you and everyone else who has read your question can attest, it's a confusing phrase to use because it does not mean the same as this:

    for ( ; NULL; ) { }
    

    The above won't loop at all. It uses the macro NULL, which is equivalent to the integer constant zero, and zero in a Boolean context is treated as false. That's not "the null condition" because there is a condition there — a condition that is always false.

    If you got this from your professor, and he is teaching a class in C++, then I suspect you misunderstood him, because this isn't something I'd expect a professor to get wrong, so go ask him to fill in the hole in your notes. If he's not a C++ professor, then maybe he simply tried to use a C++ example to illustrate some other topic. You might wish to send him a note about it so that he can clarify and present some different example in future lectures.

提交回复
热议问题