I was looking at an example and I saw this:
char *str;
/* ... */
if (!str || !*str) {
return str;
}
Does it mean it's empty or something?
str
is a char pointer. !
negates it. Basically, !str
will evaluate to true (1) when str == NULL
.
The second part is saying, (if str
points to something) evaluate to true (1) if the first character is a null char ('\0'
) - meaning it's an empty string.
Note:*str
dereferences the pointer and retrieves the first character. This is the same as doing str[0]
.
!str
means that there is no memory allocated to str
. !*str
means that str
points to an empty string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With