简体   繁体   中英

Is using NULL and '\0' in condition statements the same?

Are the following two conditions the same in C++? Are they both acceptable? I'm using Visual Studio 2013 Express, and I used NULL. My program ran fine, but on my homework grade, it was counted off because I used NULL instead of '\\0'.

 if (charArray[0] == NULL)
 if (charArray[0] == '\0')

NULL is a null pointer constant. charArray[0] is probably not a pointer. Hence you shouldn't compare it against NULL .

The C++ standard says:

The macro NULL is an implementation-defined C++ null pointer constant in this International Standard (4.10).

Permissible definitions include but are not limited to 0 , 0L , and, in C++11, nullptr . If NULL were defined as nullptr your program won't even compile.

In modern C++, you should use literals like 0 or '\\0' if you want zero, and nullptr if you want the null pointer. NULL shouldn't really be used.

Without knowing what language you're using (it looks like C/C++), it's hard to say for sure.

In C/C++, NULL is usually defined in an included header file, using a #define statement. It is usually defined as 0, but maybe your instructor docked you marks because it could theoretically be defined as something else?

Also, 0 and '\\0' are of different types, even though they have the same binary representation, which might also be a reason to make the distinction.

It will work in older standard versions of the language (C++98 and C++03), where legal definition of NULL were limited to integer constant expressions with zero value. But using NULL in non-pointer contexts would only make the code harder to read. NULL should be reserved to pointer contexts only.

C++11 allowed additional possibilities for how NULL can be defined. Some legal definitions of NULL in C++11 will not even compile in integer context.

In other words, NULL should never be used the way you use it in your code sample.

'\\0' is the "null character". It is 1 byte with a value is 0x00.

NULL is the "null pointer". On a 32-bit machine it probably 4 bytes with a value of 0x00000000. On a 64-bit machine it probably 8 bytes with a value of 0x0000000000000000. (Like int it is left up to the writer of the compiler to decide how they implement it, and what size it is.)

So, while both versions of code do return the same bool answer, they could allocate different amounts of memory, and call different comparison functions in order to perform this comparison. (If you turn off all compiler optimizations, it is guaranteed that this is the case. If you turn on compiler options, a smart compiler might optimize both versions into the same code.)

Personally, I prefer to use the same types in my comparisons. So I would always write == '\\0' for a char type and write == NULL for a pointer type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM