Since C-style strings are always terminated with the null character (\0), you can check whether the string is empty by writing

do {
   ...
} while (url[0] != '\0');

Alternatively, you could use the strcmp function, which is overkill but might be easier to read:

do {
   ...
} while (strcmp(url, ""));

Note that strcmp returns a nonzero value if the strings are different and 0 if they're the same, so this loop continues to loop until the string is empty.

Hope this helps!

Answer from templatetypedef on Stack Overflow
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 117372-check-if-string-null.html
Check if string is null
char *p=NULL; if (!p) printf("p is null"); else printf("p is not null"); keep in mind that if i didn't write the assignment (p=NULL) its value would be undefined and the if statement wouldn't work. in a string '\0' is the terminating null byte, you can check when a string is finished but you need to dereference the address:
🌐
Reddit
reddit.com › r/learnprogramming › how do i compare a string to null in c++?
r/learnprogramming on Reddit: How do I compare a string to NULL in C++?
September 27, 2019 -

I need to simply check if an object in a string vector is null or not:

std::vector<string> vliwSlots;
//Some inserts...
string vliwSlot = vliwSlots.at(i); //in a for loop
if(vliwSlot != NULL){ //error on this line
    //Do stuff
}

When I compile this I get the following error at :

no match for ‘operator!=’ (operand types are ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ and ‘long int’)

It seems it thinks NULL is a long int. How the heck do I check if a string is null?

🌐
GameDev.net
gamedev.net › forums › topic › 421554-c-how-do-i-check-if-a-stdstring-is-null › 3807807
[C++] How do I check if a std::string is NULL? - General and Gameplay Programming - GameDev.net
October 30, 2006 - Before posting, review our community guidelines. Support GameDev.net with a monthly GDNet+ subscription! ... Chat in the GameDev.net Discord! ... void setName(std::string newName) { if (newName==NULL) this->name = GenerateAutoName(); else this->name = newName; } The idea is if the function recieves a NULL as a parameter, it will set name to default. However, it seems that std::string has an overriden operator == which doesn't allow me to check if it is null.
🌐
Sabe
sabe.io › blog › c-check-string-empty
How to Check if a String is Empty in C
March 19, 2022 - Therefore, if the length of the string is 0, then the first character of the string is a null character. ... CLIKE#include <stdio.h> #include <string.h> int main() { char string[] = ""; if (string[0] == '\0') { printf("String is empty"); } else ...
🌐
C For Dummies
c-for-dummies.com › blog
Null Versus Empty Strings | C For Dummies Blog
Array null[] at Line 7 is a null string. It’s declared, allocated 5 elements, but never assigned any values. The strcmp() function at Line 9 compares the two char arrays. ... You might think that you could use the strlen() function to obtain the string lengths and compare them, but that logic doesn’t apply: strlen() returns zero for the empty[] array, even though that array has a single element.
🌐
TutorialsPoint
tutorialspoint.com › how-to-check-if-a-variable-is-null-in-c-cplusplus
How to check if a variable is NULL in C/C++?
In C/C++, there is no special method for comparing NULL values. We can use if statements to check whether a variable is null or not.Here, we will attempt to open a file in read mode that does not exist on the system. In this case, the function will r
🌐
Cplusplus
cplusplus.com › reference › string › string › empty
std::string::empty
Returns whether the string is empty (i.e. whether its length is 0). This function does not modify the value of the string in any way. To clear the content of a string, see string::clear.
Find elsewhere
🌐
Quora
quora.com › How-do-I-check-if-a-string-is-empty-in-C
How to check if a string is empty in C - Quora
Answer (1 of 10): Many of the answers I have seen here are well-intentioned, but also use outdated methods. Assuming that all strings are ANSI formatted is an incomplete answer. Well, first of all, C does not have a string type. A string in C is essentially a set of characters in contiguous mem...
🌐
Reactgo
reactgo.com › home › how to check if a string is empty in c
How to check if a string is empty in C | Reactgo
March 8, 2023 - #include <stdio.h> #include <string.h> ... \0, so we can check if a given string is empty or not by verifying if a string contains the first character is \0....
🌐
Quora
quora.com › How-can-I-easily-check-that-a-string-or-c_string-is-null-terminated
How to easily check that a string or c_string is null terminated - Quora
Answer (1 of 3): This is really a question about defensive programming I think. TLDR: Use the “n” variants of the library functions: strncpy, strnlen, etc. When coding with null terminated strings, you would like to assure that your code never reads or writes past the end of the space supposedl...
Top answer
1 of 6
30

In C and C++, pointers are inherently unsafe, that is, when you dereference a pointer, it is your own responsibility to make sure it points somewhere valid; this is part of what "manual memory management" is about (as opposed to the automatic memory management schemes implemented in languages like Java, PHP, or the .NET runtime, which won't allow you to create invalid references without considerable effort).

A common solution that catches many errors is to set all pointers that don't point to anything as NULL (or, in correct C++, 0), and checking for that before accessing the pointer. Specifically, it is common practice to initialize all pointers to NULL (unless you already have something to point them at when you declare them), and set them to NULL when you delete or free() them (unless they go out of scope immediately after that). Example (in C, but also valid C++):

void fill_foo(int* foo) {
    *foo = 23; // this will crash and burn if foo is NULL
}

A better version:

void fill_foo(int* foo) {
    if (!foo) { // this is the NULL check
        printf("This is wrong\n");
        return;
    }
    *foo = 23;
}

Without the null check, passing a NULL pointer into this function will cause a segfault, and there is nothing you can do - the OS will simply kill your process and maybe core-dump or pop up a crash report dialog. With the null check in place, you can perform proper error handling and recover gracefully - correct the problem yourself, abort the current operation, write a log entry, notify the user, whatever is appropriate.

2 of 6
8

The other answers pretty much covered your exact question. A null check is made to be sure that the pointer you received actually points to a valid instance of a type (objects, primitives, etc).

I'm going to add my own piece of advice here, though. Avoid null checks. :) Null checks (and other forms of Defensive Programming) clutter code up, and actually make it more error prone than other error-handling techniques.

My favorite technique when it comes to object pointers is to use the Null Object pattern. That means returning a (pointer - or even better, reference to an) empty array or list instead of null, or returning an empty string ("") instead of null, or even the string "0" (or something equivalent to "nothing" in the context) where you expect it to be parsed to an integer.

As a bonus, here's a little something you might not have known about the null pointer, which was (first formally) implemented by C.A.R. Hoare for the Algol W language in 1965.

I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.

🌐
Wikihow
wikihow.com › computers and electronics › software › programming › c programming languages › how to check null in c: 7 steps (with pictures) - wikihow
How to Check Null in C: 7 Steps (with Pictures) - wikiHow
June 9, 2025 - The main disadvantage to the PTR == NULL method is the chance that you'll accidentally type ptr = NULL instead, assigning the NULL value to that pointer. This can cause a major headache.
🌐
Medium
medium.com › @deepakha1993 › c-string-null-empty-and-white-space-check-8d5409c04f6c
C# - String Null, Empty and White space check | by Deepak H A | Medium
September 21, 2024 - The value of a string variable can be either null or empty or can contain white spaces. Frequent checks need to be carried out on any string variable against these possibilities. To perform these checks, dot net framework class library provides two extension methods for string. ... To clearly understand, let’s run the below code: (Create a console application in your preferred IDE. I used Visual Studio 2022) string name = ""; if (string.IsNullOrEmpty(name)) { Console.WriteLine("Name is empty"); } if (string.IsNullOrWhiteSpace(name)) { Console.WriteLine("Name has whitespaces"); }
🌐
Quora
quora.com › How-can-I-check-for-input-string-validity-Check-if-the-input-string-is-null-if-it-is-return-null-in-C-language
How can I check for input string validity? Check if the input string is null; if it is, return null in C language.
Answer (1 of 4): [code]const char* is_something(const char* str) { if(!str || !*str) return NULL; return str; } [/code]A string in C is a null -terminate sequence of character. A null string is a string containing just a terminator. And since string are passed around as pointer to the sequence...
🌐
Quora
quora.com › How-do-you-check-for-an-empty-string-and-null-character-in-the-C-programming-language
How to check for an empty string and null character in the C# programming language - Quora
Answer: You first have to explain what “ermpty” means for you. However, i always use [code ]String.IsNullOrEmpty[/code] if i want to know if the string is either null or an empty string(“”), so contains no letters, not even white-spaces. You also can use [code ]String.IsNullOrWhiteSpac[/code]`. T...