NULL
來自 cppreference.com
定義於標頭檔案 <clocale> |
||
定義於標頭檔案 <cstddef> |
||
定義於標頭檔案 <cstdio> |
||
定義於標頭檔案 <cstdlib> |
||
在標頭檔案 <cstring> 中定義 |
||
定義於標頭檔案 <ctime> |
||
在標頭檔案 <cwchar> 中定義 |
||
#define NULL /* 實現定義 */ |
||
宏 NULL
是實現定義的空指標常量。
目錄 |
[編輯] 可能的實現
#define NULL 0 // since C++11 #define NULL nullptr |
[編輯] 注意
在 C 中,宏 NULL
可能具有型別 void*,但在 C++ 中不允許,因為空指標常量不能具有該型別。
[編輯] 示例
執行此程式碼
#include <cstddef> #include <iostream> #include <type_traits> #include <typeinfo> class S; int main() { int* p = NULL; int* p2 = static_cast<std::nullptr_t>(NULL); void(*f)(int) = NULL; int S::*mp = NULL; void(S::*mfp)(int) = NULL; auto nullvar = NULL; // may trigger a warning when compiling with gcc/clang std::cout << "The type of nullvar is " << typeid(nullvar).name() << '\n'; if constexpr(std::is_same_v<decltype(NULL), std::nullptr_t>) std::cout << "NULL implemented with type std::nullptr_t\n"; else std::cout << "NULL implemented using an integral type\n"; [](...){}(p, p2, f, mp, mfp); // < suppresses "unused variable" warnings }
可能的輸出
The type of nullvar is long NULL implemented using an integral type
[編輯] 參閱
nullptr (C++11) | 指定空指標值的指標字面量 |
(C++11) |
空指標字面量 nullptr 的型別 (typedef) |
C 文件 關於 NULL
|