I am writing some header files that will be included in C ++ code, and also included in C code. I would prefer the header source code to be consistent and always use "nullptr" rather than NULL (or integer constant 0) in these headlines.
This makes it possible to add a piece of code, for example:
#ifndef __cplusplus
#define nullptr 0
#endif
Or alternatively:
#include <stdio.h>
#ifndef __cplusplus
#define nullptr NULL
#endif
Edit: Or from Ben Voigt's suggestion:
#ifndef __cplusplus
static const void *nullptr = 0;
#endif
The question is, what is the disadvantage for this? Is there any reason why nullptr was added in C ++ but not in C? This is similar to making the code more consistent and readable when C ++ and C need to interact.
Edit: sample header
#ifdef __cplusplus
extern "C" {
#endif
extern struct MyStruct *globalMyStruct;
extern int myFunc(struct MyStruct *ptr,int arg);
#ifdef __cplusplus
}
#endif
#define MYMACRO(x) ((globalMyStruct!=nullptr) ? myFunc(globalMyStruct,(x)) : -1)
MyStruct ++, C MYMACRO. C ++. C, - nullptr.
, C ++, (, , ++). ++ C.