Is there a drawback related to C # define nullptr in C code?

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      /* or ((void *)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.

+6
1

:

  • NULL/0 nullptr ( ). , nullptr (macro) nullptr ( ) , ?
  • ( C, )

: -, ++ ( C):

#if __cplusplus >= 201103L || (__cplusplus < 200000 && __cplusplus > 199711L)
//use C++ 11 nullptr
#else
    struct nullptr_t
    {
        template <class T>
            operator T* (){return (T*)0;}
    }nullptr;
#endif

nullptr .

+1

All Articles