What is the difference between NULL, '\0' and 0?
Note: This answer applies to the C language, not C++.
Null Pointers
The integer constant literal 0
has different meanings depending upon the context in which it's used. In all cases, it is still an integer constant with the value 0
, it is just described in different ways.
If a pointer is being compared to the constant literal 0
, then this is a check to see if the pointer is a null pointer. This 0
is then referred to as a null pointer constant. The C standard defines that 0
cast to the type void *
is both a null pointer and a null pointer constant.
Additionally, to help readability, the macro NULL
is provided in the header file stddef.h
. Depending upon your compiler it might be possible to #undef NULL
and redefine it to something wacky.
Therefore, here are some valid ways to check for a null pointer:
if (pointer == NULL)
NULL
is defined to compare equal to a null pointer. It is implementation defined what the actual definition of NULL
is, as long as it is a valid null pointer constant.
if (pointer == 0)
0
is another representation of the null pointer constant.
if (!pointer)
This if
statement implicitly checks "is not 0", so we reverse that to mean "is 0".
The following are INVALID ways to check for a null pointer:
int mynull = 0;
<some code>
if (pointer == mynull)
To the compiler this is not a check for a null pointer, but an equality check on two variables. This might work if mynull never changes in the code and the compiler optimizations constant fold the 0 into the if statement, but this is not guaranteed and the compiler has to produce at least one diagnostic message (warning or error) according to the C Standard.
Note that the value of a null pointer in the C language does not matter on the underlying architecture. If the underlying architecture has a null pointer value defined as address 0xDEADBEEF, then it is up to the compiler to sort this mess out.
As such, even on this funny architecture, the following ways are still valid ways to check for a null pointer:
if (!pointer)
if (pointer == NULL)
if (pointer == 0)
The following are INVALID ways to check for a null pointer:
#define MYNULL (void *) 0xDEADBEEF
if (pointer == MYNULL)
if (pointer == 0xDEADBEEF)
as these are seen by a compiler as normal comparisons.
Null Characters
'\0'
is defined to be a null character - that is a character with all bits set to zero. '\0'
is (like all character literals) an integer constant, in this case with the value zero. So '\0'
is completely equivalent to an unadorned 0
integer constant - the only difference is in the intent that it conveys to a human reader ("I'm using this as a null character.").
'\0'
has nothing to do with pointers. However, you may see something similar to this code:
if (!*char_pointer)
checks if the char pointer is pointing at a null character.
if (*char_pointer)
checks if the char pointer is pointing at a non-null character.
Don't get these confused with null pointers. Just because the bit representation is the same, and this allows for some convenient cross over cases, they are not really the same thing.
References
See Question 5.3 of the comp.lang.c FAQ for more.
See this pdf for the C standard. Check out sections 6.3.2.3 Pointers, paragraph 3.
How to set null value to int in c#?
In .Net, you cannot assign a null
value to an int
or any other struct. Instead, use a Nullable<int>
, or int?
for short:
int? value = 0;
if (value == 0)
{
value = null;
}
Further Reading
- Nullable Types (C# Programming Guide)
C type conversion between NULL and integer
This will probably compile and execute correctly since you're casting NULL to int (i.e., the compiler assumes you know what you're doing), but NULL is intended to be used with pointers. Since your structure fields are ints, just set them equal to zero to initialize them ("frameTable[i].lv1Index = 0;"). If you want to indicate that they are not valid indices yet, then set them to -1 or some other invalid value.
Nullable values in C++
Boost.Optional probably does what you need.
boost::none
takes the place of your CNullValue::Null()
. Since it's a value rather than a member function call, you can do using boost::none;
if you like, for brevity. It has a conversion to bool
instead of IsNull
, and operator*
instead of GetValue
, so you'd do:
void writeToDB(boost::optional<int> optional_int) {
if (optional_int) {
pass *optional_int to database;
} else {
pass null to database;
}
}
But what you've come up with is essentially the same design, I think.
Why type int is never equal to 'null'?
If you want your integer variable to allow null values, declare it to be a nullable type:
int? n = 0;
Note the ?
after int, which means that type can have the value null
. Nullable types were introduced with v2.0 of the .NET Framework.
Why int can't be null? How does nullable int (int?) work in C#?
int
is a primitive type and only ReferenceType
s (objects) are nullable. You can make an int
nullable by wrapping it in an object:
System.Nullable<int> i;
-or-
int? i;
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/nullable-types/using-nullable-types
When should one use nullable types in c#?
From Using Nullable Types (C# Programming Guide) (Link updated circa 2018)
For an example of when you might use a
nullable type, consider how an
ordinary Boolean variable can have two
values: true and false. There is no
value that signifies "undefined". In
many programming applications, most
notably database interactions,
variables can exist in an undefined
state. For example, a field in a
database may contain the values true
or false, but it may also contain no
value at all. Similarly, reference
types can be set to null to indicate
that they are not initialized.
How to set value type to null in c#?
So value types by default can't be set to null, there are however ways to get them to set to null. To solve your issue you would need to do this:int? i = null;
This comes from the Microsoft docs here:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/nullable-types/
Value Types versus Reference Types can be found here:
https://www.tutorialsteacher.com/csharp/csharp-value-type-and-reference-type
Related Topics
C++ - <Unresolved Overloaded Function Type>
Same Random Numbers Every Time I Run the Program
Lifetime of Lambda Objects in Relation to Function Pointer Conversion
Request for Member '...' Is Ambiguous in G++
How to Use C++ Preprocessor Stringification on Variadic MACro Arguments
Is Msiopenproduct the Correct Way to Read Properties from an Installed Product
Opencv How to Select a Region of Image Irregularly with Mouse Event? C/C++
Cmake: Failed to Run Msbuild Command: Msbuild.Exe
How Computer Does Floating Point Arithmetic
How to Restart My Own Qt Application
_Iterator_Debug_Level Value '0' Doesn't Match Value '2'
Does C++ Contain the Entire C Language
How to Check If Window Is "Always on Top"
Cmake Cannot Determine Linker Language for Target
Can Raw Pointers Be Used Instead of Iterators with Stl Algorithms for Containers with Linear Storage