Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

char

pointers

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

like image 797
Aan Avatar asked Nov 01 '11 17:11

Aan


People also ask

What is the char value for empty string?

The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters.

How do I check if a char array is empty?

The easiest/fastest way to ensure that a C string is initialized to the empty string is to simply set the first byte to 0. char text[50]; text[0] = 0; From then, both strlen(text) and the very-fast-but-not-as-straightforward (text[0] == 0) tests will both detect the empty string.

How do I check if a char is empty or null in C#?

IsWhiteSpace(String, Int32) Method. This method is used to check whether a character in the specified string at the specified position can be categorized as whitespace or not. It returns True when the character is a whitespace character otherwise it returns False.

Can a char be empty?

An empty char value does not belong to any char, so Java gives a compile-time error. To create an empty char, we either can assign it a null value \0 or default Unicode value \u0000 .


7 Answers

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");
}
like image 198
codemaker Avatar answered Oct 02 '22 22:10

codemaker


My preferred method:

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

Probably more common:

if (strlen(ptr) == 0) // empty string
like image 25
Mark Ransom Avatar answered Oct 02 '22 21:10

Mark Ransom


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.

like image 26
Alok Save Avatar answered Oct 02 '22 22:10

Alok Save


if (!*ptr) { /* empty string  */}

similarly

if (*ptr)  { /* not empty */ }
like image 26
alvin Avatar answered Oct 02 '22 21:10

alvin


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

like image 45
Basile Starynkevitch Avatar answered Oct 02 '22 22:10

Basile Starynkevitch


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

like image 20
bhuwansahni Avatar answered Oct 02 '22 21:10

bhuwansahni


Give it a chance:

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

like image 40
ikm104 Avatar answered Oct 02 '22 21:10

ikm104