-1

I am getting these results. What am I doing wrong?

const char *c = "\0";
cout << (c == NULL); // false
cout << (c == nullptr); //false
4
  • 7
    I think you're confusing what a pointer is versus what it points to. This is an extremely important distinction that must be understood before using pointers in any capacity. Here c points to an empty C string, but it is absolutely not a "null pointer" in any way. It is assigned. Imagine in other languages like Java this is null vs "". Commented Apr 29, 2023 at 17:35
  • "\0" isn't what you believe it is. Commented Apr 29, 2023 at 17:35
  • "\0" is a string literal. Why do you expect it to be null? Are you maybe confusing it with a null-terminator? Commented Apr 29, 2023 at 17:35
  • @NathanOliver Yeah, I am actually confused of the concept of null-terminator. I thought the null-terminator actually enforce the "c" to point to nothing. Thanks. Commented Apr 29, 2023 at 17:43

3 Answers 3

6

All literal strings in C++ are really constant arrays of characters, including the null-terminator character.

So doing e.g.

const char* c = "\0";

is somewhat equivalent to

char const internal_string_array[] = { '\0', '\0' };
const char* c = &internal_string_array[0];

So a pointer to a literal string will never be a null pointer.

Also there's a difference between a null pointer and the string null terminator character. A null pointer is a pointer which have been initialized to point to nowhere. A string null terminator character is the character '\0'. These two are different things.


If you want to check to see if a C-style string (using pointers or arrays) is empty, then you first need to make sure it's not a null-pointer (if you're using pointers) and then check to see if the first character is the string null terminator character:

if (c == nullptr)
{
    // No string at all!
}
else if (c[0] == '\0')
{
    // String exists, but is empty
}
else
{
    // String exists, and is not empty
}

For std::string objects (which you should use for almost all your strings) then you don't need the null-pointer check:

if (str.empty())
{
    // String is empty
}
else
{
    // String is not empty
}

Also note that even if a string is not empty, it might contain unprintable characters (like the ASCII control characters), or characters that are "invisible" (like for example space or tab). So if printing the non-empty string it might not show anything.

Sign up to request clarification or add additional context in comments.

1 Comment

It may be worth mentioning that std::string may even contain null chars and be considered to be non-empty: std::string str("\0"sv); assert(!str.empty()); assert(str.at(0) == '\0'); won't trigger an assertion error in debug mode. Your code for C-style strings has a different behaviour in this case.
3

The pointer c is not a null pointer because it points to the string literal "\0".

const char *c = "\0";

To declare a null pointer you could write for example

const char *c = nullptr;

or

const char *c = 0;

If you want to check whether the pointer points to a null string (provided that it indeed points to a string) then write

cout <<  !*c;

3 Comments

A null pointer doesn't point to a null (= empty?) string, but you can't do !*c on it.
@Evg Your comment does not make sense. Reread my answer one more. Nevertheless I have updated the answer.
If it hadn't made sense, you needn't have updated the answer.
1

If you're asking about the string's length, then in C++ you'd use std::string which can tell you straight up if it is empty(), or in C you'd check with strlen(c) == 0.

An empty string is actual data, even if it's not necessarily a whole lot of data. It is completely different from a "null" pointer which is one that does not point at any data at all.

The \0 part of the "\0" string is called "NUL" or "NUL byte" to avoid confusion with "NULL" as in pointer, and is how it's described in the ASCII standard.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.