Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What ! (char*) means in C

Tags:

c

char

pointers

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?

like image 837
PTN Avatar asked Jul 31 '15 03:07

PTN


2 Answers

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].

like image 174
pushkin Avatar answered Sep 19 '22 01:09

pushkin


!str means that there is no memory allocated to str. !*str means that str points to an empty string.

like image 25
unxnut Avatar answered Sep 18 '22 01:09

unxnut