What is the difference between str==NULL and str[0]==’\0′ in C?

str==NULL tells you whether the pointer is NULL.

str[0]=='\0' tells you if the string is of zero-length.

In that code, the test:

if ((str == NULL) || (str[0] == '\0'))

is used to catch the case where it is either NULL or has zero-length.


Note that short-circuiting plays a key role here: The point of the test is to make sure that str is a valid c-string with length at least 1.

  • The second test str[0] == '\0' will only work if str is not NULL.
  • Therefore, the first test str == NULL is needed to break out early when str is NULL.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)