Skip to main content
Engineering LibreTexts

2.10: Invalid and NULL Pointers

  • Page ID
  • Invalid pointers

    A pointer should point to a valid address but not necessarily to valid elements (like for arrays). These are called invalid pointers. Uninitialized pointers are also invalid pointers.

    int *ptr1;

    int arr[10];

    int *ptr2 = arr+20;

    Here, ptr1 is uninitialized so it becomes an invalid pointer and ptr2 is out of bounds of arr so it also becomes an invalid pointer.
    (Note: invalid pointers do not necessarily raise compile errors)

    NULL pointers

    Additional information on NULL Pointers is available.

    Null pointer is a pointer which point nowhere and not just an invalid address.
    Following are 2 methods to assign a pointer as NULL;

    int *ptr1 = 0;

    int *ptr2 = NULL;

    Please note that 0 in the above code is used in a pointer context, and it is different from 0 as an integer

    Adapted from:
    "Pointers in C/C++ with Examples" by Abhirav Kariya, Geeks for Geeks is licensed under CC BY 4.0

    • Was this article helpful?