73

How can I check if char* variable points to an empty string?

AlexV
  • 22,658
  • 18
  • 85
  • 122
Aan
  • 12,247
  • 36
  • 89
  • 150

7 Answers7

125

Check if the first character is '\0'. You should also probably check if your pointer is NULL.

char *c = "";
if ((c != NULL) && (c[0] == '\0')) {
   printf("c is empty\n");
}

You could put both of those checks in a function to make it convenient and easy to reuse.

Edit: In the if statement can be read like this, "If c is not zero and the first character of character array 'c' is not '\0' or zero, then...".

The && simply combines the two conditions. It is basically like saying this:

if (c != NULL) { /* AND (or &&) */
    if (c[0] == '\0') {
        printf("c is empty\n");
    }
}

You may want to get a good C programming book if that is not clear to you. I could recommend a book called "The C Programming Language".

The shortest version equivalent to the above would be:

if (c && !c[0]) {
  printf("c is empty\n");
}
alk
  • 69,737
  • 10
  • 105
  • 255
codemaker
  • 1,682
  • 1
  • 11
  • 13
  • @Zack: What `c && c[0]` means? Can you explain it please? – Aan Nov 01 '11 at 18:15
  • Personally I would separate the check for a NULL pointer from the check for an empty string; the two aren't really related. "more portable" probably isn't really what you were trying to say, since all of the code you present here is valid C (and C++ as well). – Mark Ransom Nov 01 '11 at 19:19
  • 11
    Writing `c != NULL` instead of just `c` gets you dinged if I'm reviewing the code, because the two are exactly the same (as long as `c` is a pointer) and the shorter form is the standard C idiom. Nonidiomatic forms should only be used when you want to draw a human reader's attention to something funny going on, but there is nothing funny going on here, so, idiom is preferred. – zwol Nov 01 '11 at 19:51
  • 4
    One character shorter than your "shortest version" would be `if (c && !*c)`. – Mark Ransom Feb 22 '16 at 13:22
15

My preferred method:

if (*ptr == 0) // empty string

Probably more common:

if (strlen(ptr) == 0) // empty string
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 8
    Note that the second, "more common" possibility is also unnecessarily slower. – jpalecek Nov 01 '11 at 17:58
  • 4
    @jpalecek, the slowness doesn't often matter and `strlen` will be easier to understand if you're not used to the first form. – Mark Ransom Nov 01 '11 at 18:01
  • @Adban, on this site the best way to thank someone is to hit the uparrow next to the answer :) – Mark Ransom Nov 01 '11 at 19:16
  • 1
    @MarkRansom: You asked me nothing hah! :) take +1. – Aan Nov 01 '11 at 23:14
  • What if `ptr` is null? – Romário Jul 19 '16 at 13:17
  • 3
    @Romário the question specifically asked "points to an empty string", so that's the question I answered; a null pointer is a different thing entirely. You can combine a test for null with the others I gave above, and if you put it first the short-circuiting rules will keep you from dereferencing the null pointer. `if (ptr == nullptr || *ptr == 0)` – Mark Ransom Jul 19 '16 at 14:00
9

Check the pointer for NULL and then using strlen to see if it returns 0.
NULL check is important because passing NULL pointer to strlen invokes an Undefined Behavior.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
6

An empty string has one single null byte. So test if (s[0] == (char)0)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
2
if (!*ptr) { /* empty string  */}

similarly

if (*ptr)  { /* not empty */ }
alvin
  • 1,176
  • 8
  • 15
  • 1
    @igraczech, hmm, this should work for the case char *str = ""; if (!*str) { printf("empty\n"); }. did I miss some subtle case? – alvin Dec 03 '16 at 08:10
  • Sorry, you're right – it was my stupid C-pointer oversight. I had the case where str was pointing to an address containing '\0' only. So the pointer (*str) was non-null where the value (content of address pointed to by str) was null. – igraczech Dec 05 '16 at 14:29
2

I would prefer to use the strlen function as library functions are implemented in the best way.

So, I would write if(strlen(p)==0) //Empty string

bhuwansahni
  • 1,834
  • 1
  • 14
  • 20
  • 2
    Great idea, lets iterate over the entire string (which, for all you know, could be 2GB in size) just to find out if it contains at least one character! – Lily Ballard Nov 01 '11 at 18:31
  • 3
    @KevinBallard while you are right there is no need to be a jerk about it. – marsh Oct 12 '16 at 17:13
1

Give it a chance:

Try getting string via function gets(string) then check condition as if(string[0] == '\0')

ikm104
  • 43
  • 4