Understanding why null and zero

This is this program I am reading it goes like

if ( char* pointer == null )

something1

else

something2

if ( char* pointer == 0 and size of array != value )

something3


Isn't char* pointer == null and == 0 the same thing? Why use two different Things?
closed account (48T7M4Gy)
Why use two different Things?


It's worse than that there are 5 and possibly even more. '\0', null, 0, NULL and nullptr, all sort of doing the same thing but only one should be used in modern C++ parlance i.e. nullptr

http://stackoverflow.com/questions/1282295/what-exactly-is-nullptr

possibly even more

Definitely, in conditions the comparison of pointer to value does not always have to be explicit:
1
2
3
4
5
6
7
8
9
10
T * ptr = ...
if ( ptr ) {
  // use ptr
}

// and
Base * objp = ...
if ( auto derp = dynamic_cast<Derived*>(objp) ) {
  // use derp
}



Why different things? Legacy and clarity. C++ inherits from C. In the beginning someone decided that integer value 0 is used with "empty pointers". However, when the code has many integer 0's the pointer's 0 does not stand out. C has preprocessor macros and C libraries started to define macro NULL. When code shows word NULL, it is more obvious that it is not doing usual integer math.

Whoever invented code pages (ASCII table, UTF-8, etc) desided that (unprintable) character \0 has numeric value 0. Again, clarity; all zeros are zero, but here I have character rather than integer.

As said, modern C++ code should use nullptr.
Topic archived. No new replies allowed.