Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 21 Jun, 2022
View Discussion
Improve Article
Save Article

What is undeclared Error: When we use some constant in our program maybe 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: 

CPP




using namespace std;
 
// Driver Code
int main()
{
    // NULL declared
    int* num = NULL;
    return 0;
}

Time complexity: O(1)

Auxiliary Space: O(1)

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 for storing 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 to any address, so the simplest solution is simply assigning 0. Below code shows its implementation: 

CPP




using namespace std;
 
// Driver Code
int main()
{
    int* num = 0;
    return 0;
}

Time complexity: O(1)

Auxiliary Space: O(1)

  • 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 its implementation: 

C




#include <stddef.h>
 
// Driver Code
int main()
{
    int* num = NULL;
    return 0;
}

Time complexity: O(1)

Auxiliary Space: O(1)

  • Include iostream Header File: In C++ if we want to execute our program without NULL undeclared error, we can simply include iostream in our program and make it happen without any error. Below code shows its implementation: 

CPP




#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    int* num = NULL;
    return 0;
}

Time complexity: O(1)

Auxiliary Space: O(1)

  • #define NULL 0: Using #define NULL 0 line in our program, we can solve the NULL undeclared error. Below code shows its implementation: 

CPP




#define NULL 0
using namespace std;
 
// Driver Code
int main()
{
    int* num = NULL;
    return 0;
}

Time complexity: O(1)

Auxiliary Space: O(1)

  • In newer C++(C++11 and higher):: nullptr is a built-in constant, so we can use it instead of using NULL.

CPP




#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    int* num = nullptr;
    return 0;
}

Time Complexity: O(1) // Declaration/Initialization takes constant time
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!