Meaning and Definition of NULL

Need help with C, C++, perl, python, etc?

Meaning and Definition of NULL

Postby bentHnau » 2018-12-29 04:41

When I look in stddef.h. I see that NULL has multiple definitions:

Code: Select all
403 #define NULL __null
404 #else   /* G++ */
405 #ifndef __cplusplus
406 #define NULL ((void *)0)
407 #else   /* C++ */
408 #define NULL 0

How is the appropriate definition for a given context chosen? And what exactly does ((void *) 0) mean? My book says that it is simply an address, but what does the 0 represent?
Debian Stretch x64 on a Dell XPS 17 laptop
bentHnau
 
Posts: 125
Joined: 2014-01-07 01:43
Location: California

Re: Meaning and Definition of NULL

Postby peter_irich » 2018-12-29 12:55

I think it is simply data type (void *). There are values of the different types: int, double, float and others
and pointers to this types, its differs by length and format, so there is and type (void *).
"0" means the this pointer does not dereference.

Peter.
peter_irich
 
Posts: 1241
Joined: 2009-09-10 20:15
Location: Saint-Petersburg, Russian Federation

Re: Meaning and Definition of NULL

Postby ruwolf » 2019-01-28 01:42

It depends on version of compiler.
If you run it as C++, definition is simple:
0
If you run it as C (which is not C++, #ifndef is command of preprocessor meaning "if not defined"), it is pointer to 0. It is used as type void pointer, because this type of pointer can be in C converted to pointer to object of different type without explicit cast, e.g. implicitly.
Macro __cplusplus is defined, if compiler is running as C++; if compiler is runing as C, it is not defined.
ruwolf
 
Posts: 121
Joined: 2008-02-18 05:04
Location: Slovakia, Banovce nad Bebravou, Matice slovenskej 1260/4-7


Return to Programming

Who is online

Users browsing this forum: No registered users and 4 guests

fashionable