#ifndef NULL
#define NULL NULL
#endif
This code compiles in gcc with no warnings/errors. Can someone explain what the preprocessor is doing here?
Anywhere the compiler sees the text "NULL" it will replace it with the text "NULL". It's like doing a search-and-replace in your code for "NULL" and replacing with "NULL". Not illegal, just weird :)
The only possible reason for doing this would be to do it before including header files which themselves do something like
#ifndef NULL
#define NULL (void *)0
#endif
This would then stop the NULL from being defined like that.
It's normal:
#ifndef JON_SKEET
#define JON_SKEET JON_SKEET
#endif
This compiles too. This is because the preprocessor simply does mindless replaces. What it replaces and what it replaces with don't need to be valid identifiers.
Think of it like this: open your editor's Search & Replace window and type in "NULL" in both the Replace
and Replace with
fields. It won't give any error or warning and it will "work", even though it actually doesn't do anything. The preprocessor does the same thing.
Obviously when you try to use it:
'JON_SKEET' undeclared (first use in this function) (Each undeclared identifier is reported only once for each function it appears in.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With