NULL undeclared error in C/C++ and how to resolve it - GeeksforGeeks

21 Jun, 2020
What is undeclared Error:When we use some constant in our program may be they are built-in constant and may be created by a user according to the requirement. But when we use some constant and they are not built-in and also not defined by a user in that condition we get an undeclared error.
Below is the code that shows the example of NULL undeclared Error:
using namespace std;  // Driver Codeint main(){    // NULL declared    int* num = NULL;    return 0;}
The above code will show an error as “NULL undeclared Error”. The reason for the NULL undeclared error is “NULL” is not a built-in constant.
Why do we need NULL?When we create some pointer in our program they are used to store addresses. But an uninitialized pointer variable is very dangerous so that we can assign them NULL which means they are not pointing to any memory location so our program runs smoothly and securely.Now if NULL is not built-in constant how we can overcome the NULL undeclared error.
Below are some code which is used to remove the NULL undeclared Error:
Assign 0: Instead of assigning NULL to num we can simply assign 0 which indicate that it is not pointing any address so the simplest solution is simply assigning 0.Below code shows it’s implementation:using namespace std;  // Driver Codeint main(){    int* num = 0;    return 0;}Include “stddef.h” Header file: In stddef.h header file NULL is already defined so we can include this header file in our program and our program will compile and execute without any error.Below code shows it’s implementation:#include <stddef.h>  // Driver Codeint main(){    int* num = NULL;    return 0;}Include iostream Header File: In C++ if we want to execute our program without NULL undeclered error we can simply include iostream in our program and make it happen without any error.Below code shows it’s implementation:#include <iostream>using namespace std;  // Driver Codeint main(){    int* num = NULL;    return 0;}#define NULL 0: Usingt #define NULL 0 line in our program we can solve the NULL undeclared error.Below code shows it’s implementation:#define NULL 0using namespace std;  // Driver Codeint main(){    int* num = NULL;    return 0;}In newer C++(C++11 and higher):: nullptr is a built-in constant so we can use it instead of using NULL.#include <iostream>using namespace std;  // Driver Codeint main(){    int* num = nullptr;    return 0;}My Personal Notes
arrow_drop_upSave
Assign 0: Instead of assigning NULL to num we can simply assign 0 which indicate that it is not pointing any address so the simplest solution is simply assigning 0.Below code shows it’s implementation:using namespace std;  // Driver Codeint main(){    int* num = 0;    return 0;}
using namespace std;  // Driver Codeint main(){    int* num = 0;    return 0;}
Include “stddef.h” Header file: In stddef.h header file NULL is already defined so we can include this header file in our program and our program will compile and execute without any error.Below code shows it’s implementation:#include <stddef.h>  // Driver Codeint main(){    int* num = NULL;    return 0;}
#include <stddef.h>  // Driver Codeint main(){    int* num = NULL;    return 0;}
Include iostream Header File: In C++ if we want to execute our program without NULL undeclered error we can simply include iostream in our program and make it happen without any error.Below code shows it’s implementation:#include <iostream>using namespace std;  // Driver Codeint main(){    int* num = NULL;    return 0;}
#include <iostream>using namespace std;  // Driver Codeint main(){    int* num = NULL;    return 0;}
#define NULL 0: Usingt #define NULL 0 line in our program we can solve the NULL undeclared error.Below code shows it’s implementation:#define NULL 0using namespace std;  // Driver Codeint main(){    int* num = NULL;    return 0;}
#define NULL 0using namespace std;  // Driver Codeint main(){    int* num = NULL;    return 0;}
In newer C++(C++11 and higher):: nullptr is a built-in constant so we can use it instead of using NULL.#include <iostream>using namespace std;  // Driver Codeint main(){    int* num = nullptr;    return 0;}My Personal Notes
arrow_drop_upSave
#include <iostream>using namespace std;  // Driver Codeint main(){    int* num = nullptr;    return 0;}
C++-Exception Handling
Articles
C Language
C Programs
C++
C++ Programs
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Time Complexity and Space Complexity
Docker - COPY Instruction
Time complexities of different data structures
Difference between Class and Object
SQL | Date functions
Arrays in C/C++
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
std::sort() in C++ STL
Bitwise Operators in C/C++
Substring in C++