Skip to content
Related Articles

Related Articles

Improve Article

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

  • Difficulty Level : Basic
  • Last Updated : 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 Code
int 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:



  1. 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 Code
    int main()
    {
        int* num = 0;
        return 0;
    }
  2. 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 Code
    int main()
    {
        int* num = NULL;
        return 0;
    }
  3. 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 Code
    int main()
    {
        int* num = NULL;
        return 0;
    }
  4. #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 0
    using namespace std;
      
    // Driver Code
    int main()
    {
        int* num = NULL;
        return 0;
    }
  5. 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 Code
    int main()
    {
        int* num = nullptr;
        return 0;
    }
    Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.



    My Personal Notes arrow_drop_up
Recommended Articles
Page :