C++之‘nullptr’ was not declared in this scope

在vim里面写了一个简单cpp文件,为了避免野指针,需要指针初始化

char *p2 = nullptr

1、编译时报错如下





2、解决办法

编译加上

g++ -std=gnu++0x int.cpp -o int


3、C里面的null和C++里面的nullptr、NULL介绍

NULL在C++中的定义

/* Define NULL pointer value */
#ifndef NULL
    #ifdef __cplusplus
        #define NULL    0
    #else  /* __cplusplus */
        #define NULL    ((void *)0)
    #endif  /* __cplusplus */
#endif  /* NULL */
C++中 NULL在C++中被明确定义为整数0
NULL在C中的定义

#define NULL    ((void *)0)

你可能感兴趣的:(C&C++)