NULL Pointer and NUL in C programming.

Home Forums C-programming.. NULL Pointer and NUL in C programming.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #2136 Reply
    Humble
    Keymaster

    Null pointer does not point to any thing.. so, dont try to dereference.. you can use “0” or “NULL” when initializing a null pointer.. Depending on the implementation , different type of pointer which initialized to NULL gets its own value. To avoid problems in function call context,it is better to type cast “0” to its own data type ( what function definition assumes) when passing NULL pointer as an argument.

    Please note the “NULL” is different from “NUL”. That said, latter is used for representing end of string and no where related to null pointer.

    #3410 Reply
    Humble
    Keymaster

    #include
    int main()
    {
    int *p = NULL;

    if (p == NULL)
    {
    printf (“p is NULL\n”);
    }
    else
    {
    printf (“p is not NULL\n”);

    }
    }

    Above produce below result :

    [hchiramm@humbles-lap ]$ ./a.out
    p is NULL

    But, if we have below code :

    #include
    int main()
    {
    int *p = NULL;
    if (p)
    {
    printf (“p is NULL\n”);
    }
    else
    {
    printf (“p is not NULL\n”);

    }
    }

    [hchiramm@humbles-lap ]$ ./a.out
    p is not NULL

    Because NULL is represented as binary zero in a system, that said, binary zero is treated as “false” in C, so else clause is getting executed..

    In standard libike this:

    #define NULL ((void*)0)

    Dereferencing a NULL pointer will result in ‘segmentation fault” .

Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: NULL Pointer and NUL in C programming.
Your information: