How to define NULL with #define

I want to override NULL in my program, for example

#define MYNULL ((void*)0) 

But this definition does not work in the following statement:

 char *ch = MYNULL; 

Error: cannot convert from void * to char *

What would be the best way to define NULL?

+7
c ++ c null
source share
8 answers
 #ifdef __cplusplus #define MYNULL 0 #else #define MYNULL ((void*)0) #endif 

will work in both of them.

+16
source share
 #define MYNULL NULL 

is the safest, I see no reason for this, but if you really want it, go ahead. Here, how C and C ++ do it respectively:

 #define NULL 0 //C++ #define NULL ((void*)0) //C 

Generally speaking, defining 0 for NULL is a bad habit, you really want it to be part of the language. C ++ 0x addresses this.

Here is what Bjarne Stroustrup has to say about it :

Should I use NULL or 0?

In C ++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly think that it is different from 0 and / or not an integer. In predefined code, NULL was / sometimes defined as something inappropriate and therefore was / should be avoided. This is less common these days. If you must name a null pointer, call it nullptr; that is what it will call in C ++ 0x. Then "nullptr" will be the keyword.

+28
source share

What exactly is the problem with getting your NULL from where you should ?, i.e.

 #include <stddef.h> 

or

  #include <cstddef> 

as pointed out in a comment by @Johannes Rudolph, any trick you do is unlikely to be very promising evidence for things like nullptr , etc.

EDIT: while stdlib (and many others) should include a NULL , stddef is the most canonical header [and has been for decades].

PS All in all, it’s just a bad idea to get involved in such a hoax unless you have a really good reason. You did not expand the thinking that led you to feel the need to do this. If you could add some details about this, this will most likely lead to better answers. Other people who answered the question should have indicated this in their answers, but I think that FGITW, like FGITW, does better: D

EDIT 2: As pointed out by @Yossarian: The only excuse for this is if NULL is defined in the appropriate language agnostic form elsewhere in your system. Examples of this include bare-headed compilers without headers and / or if you write your own standard library from scratch. (In such a blue-bones scenario, I would go with @lilburne's answer (be sure to use 0 as much as possible))

+8
source share
 #define MYNULL 0 

will work in c ++

+2
source share

I think that anyone who does not know that setting the pointer in C / C ++ to 0 is the same as setting it to NULL, nullptr, or any other equivalent, should not mess with the code. The difference in readability between

 char* ch = NULL 

and

 char* ch = 0; 

is minimal. When it comes to expressions, forms

 if (NULL == ch) { } if (0 == ch) { } if (nullptr == ch) { } 

more readable than

 if (!ch) { } 
+1
source share

Do not do this. There is nothing that suggests that NULL should be an implementation-specific null value.

It can be a value that represents the end of memory, some special place in memory, or even an object that does not represent any value.

Doing this is very dangerous, can lead to portability corruption and, of course, will hang with editors that support code. It doesn’t buy anything, trust the definition of your library.

EDIT: Evan is right! The code itself will say β€œzero,” under the hood, the compiler can do what it wants with specific implementation details. Thanks Evan!

+1
source share

Unlike some people claim here, 0 is an absolutely correct definition for NULL in C. So you have to be careful when you give NULL as an argument to a variational function, because it can be mistaken as an integer a value of 0, ending in intolerance.

http://c-faq.com/null/null2.html

BTW, frequently asked questions about comp.lang.c are recommended for every C programmer. See here:

http://c-faq.com/null/null1.html

containing gems of almost forgotten wisdom, such as "As mentioned above, for each type of pointer there is a null pointer, and the internal values ​​of null pointers for different types can be different." This means that calloc or memset is NOT portable initialization for pointers.

0
source share
 #define NULL 0 //for C 

is an ideal definition in C

eg.

 char *ch = NULL ; *ch++ ;// will cause error 

it causes an error, since ch does not indicate anything when executing the increment statement is known by the compiler, assuming the pointer value in the LOOK-UP table is 0

if you are trying to update this pointer, then you are actually changing the contents of CODE that start at 0 of the physical address. For this reason, the first page table entry begins before the code area is not supported.

What exactly is the problem with getting your NULL from where you should ?, i.e.

 #include <stddef.h> 

or

 #include <cstddef> 

as pointed out in a comment by @Johannes Rudolph, any trick you do is unlikely to be very promising evidence for things like nullptr, etc.

EDIT: while stdlib (and many others) should include NULL, stddef is the most canonical header [and has been around for decades].

PS All in all, it’s just a bad idea to get involved in such a hoax unless you have a really good reason. You did not expand the thinking that led you to feel the need to do this. If you could add some details about this, this will most likely lead to better answers. Other people who answered the question should have indicated this in their answers, but I think that FGITW, like FGITW, does better: D

EDIT 2: As pointed out by @Yossarian: The only excuse for this is if NULL is defined in the appropriate language agnostic form elsewhere in your system. Examples of this include bare-headed compilers without headers and / or if you write your own standard library from scratch. (In such a blue-bones scenario, I would go with @lilburne's answer (be sure to use 0 as much as possible))

0
source share

All Articles