Zoey Malkov
Zoey Malkov

Reputation: 832

How can I check if a char * is empty?

How can I check if char *name is empty?

#include <stdio.h>
#include <string.h>

void store_stuff(char **name, int *age);
int main(void) {

    char *name;
    int age;

    store_stuff(&name, &age);

    printf("Name: %s\n", name);
    printf("Age: %d\n", age);

}

void store_stuff(char **name, int *age) {

    *age = 31;

    if (name == NULL) { // how can I check here if char name is empty?
        *name = "ERROR";
    }

}

Upvotes: 0

Views: 10400

Answers (3)

user2371524
user2371524

Reputation:

Your question seems clear, but the code shown with it makes it less so:

if (name == NULL) { // how can I check here if char name is empty?
    *name = "ERROR";
}

This is undefined behavior. If name really is NULL, you can't dereference it, but that's what you do in the next line.

Of course, the way you call this function, it can never happen. You call it like this:

store_stuff(&name, &age);

and the & operator always evaluates to a valid, non-NULL, pointer.


In general, it's unclear what you mean by "empty". There's a special value NULL for pointers that means "this pointer doesn't point to anything". But that doesn't mean "empty", it still has a value: NULL.

In the context of strings, empty would have a well-defined meaning. As strings in C are defined as a sequence of characters that ends with a \0 byte, an empty string has \0 as the very first character. So, assuming char *str is a string, you can check for an empty string like

if (*str == 0)

or even shorter:

if (!*str)

Be aware this will lead to undefined behavior if str is NULL (doesn't point to any string). If you don't know anything about it and have to check it indeed points to a string and that string is non-empty, you could write something like

if (!str || !*str)

which is roughly equivalent to the IsNullOrEmpty() method on a .NET string ...

Upvotes: 1

Achal
Achal

Reputation: 11921

How can I check if char *name is empty ? name is not initialized in main() function, First assign NULL to name like

char *name =  NULL; /* now its initialized */

Case-1 :

int main(void) {
    char *name =  NULL;
    store_stuff(&name, &age);
    /* code */
    return 0;
}

And then check *name content in the store_stuff() as

void store_stuff(char **name, int *age) {
    if (*name == NULL) { /* since name is double ptr here, *name means NULL in the main() if it is */
     /* error handling */
    }
 }

Case-2 : if name holds some data like char *name = "Zoey"; in main() function.

int main(void) {
    char *name = "Zoey" ;
    store_stuff(&name, &age);
    /* code */
    return 0;
}

then below condition won't true & I hope this is what you want

void store_stuff(char **name, int *age) {
    if (*name == NULL) { /* now its not NULL as it holds some data */
     /* error handling */
    }
}

Upvotes: 2

Lundin
Lundin

Reputation: 214300

Pointers aren't "empty", they either point at valid data or invalid data. A controlled way to make them point at invalid data is to assign them to NULL. Thus the correct way to write the program would be:

char *name = NULL;
int age;
store_stuff(&name, &age);

...

void store_stuff(char **name, int *age) {
  if(*name == NULL)
  {
    // handle error
  }
  ...
}

Upvotes: 7

Related Questions