Search code examples
cpointersansi-c

How to understand the meaning of char *fptr = NULL, *field;?


I would like to know what the following expression means:

char *fptr = NULL, *field;

Thanks!


Solution

  • Declare a char pointer fptr and initialize it to NULL and declare another char pointer field. The NULL is ((void *)0) just an invalid pointer value and you can use it to verify if the pointer is valid.

    Assigning NULL to a pointer helps you avoid dereferencing an uninitialized pointer, for example malloc returns NULL on failure.

    That way if you check your pointer against NULL after a call to malloc, you prevent undefined behavior.