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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

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

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.