[C++] nullptr
Updated:
개요
- NULL은 0으로 define되어 있는 키워드이므로 숫자 0을 의미하는지 포인터 값 0을 의미하는지 구분 불가
- nullptr은 포인터 값 0을 의미
예제
- 코드
#include <cstdlib> #include <iostream> using namespace std; int main() { int i = 0; if (i == NULL) { cout << 1 << endl; } /* compile error if (i == nullptr) { cout << 2 << endl; } */ char *pc1 = NULL; if (pc1 == NULL) { cout << 3 << endl; } if (pc1 == nullptr) { cout << 3 << endl; } char *pc2 = nullptr; if (pc2 == NULL) { cout << 3 << endl; } if (pc2 == nullptr) { cout << 3 << endl; } return 0; }
- 실행 결과
test.cpp: In function ‘int main()’: test.cpp:8:18: warning: NULL used in arithmetic [-Wpointer-arith] 8 | if (i == NULL) { | ^~~~ 1 3 3 3 3