Computer Programming
C Programming
The Difference Between

What is difference between zero value and NULL strings?

User Avatar
Wiki User
October 21, 2009 3:00PM


Programming languages store data in different data types. A zero value would belong to a numeric data type such as float, single, double, integer, long, etc. A null string is a variable that references a string (sequence of characters) that is not yet pointing to anything.


A null string can also be a string of zero length.


Several programming languages including C and C++ and several assembly languages store strings in "ASCIZ" format, also called "C string" format.
In those languages, a string variable contains only a pointer to the first character in the string, and the end-of-string is marked with the ASCII NUL character.
A programmer using strings in "ASCIZ" format must be aware of the two different ways that a string variable may store no characters:

One way a ASCIZ variable may store no characters:
A string variable may contain the null pointer constant, which is represented by the constant integer 0. All such "null pointers" point to the same location, represented by the constant integer 0, and often called NULL.
No valid string is ever located at that location.
For example,
char * winner_name = 0;
or equivalently
char * winner_name = NULL;
Typically a programmer must write explicit code to check for this possibility first.
For example,
if( winner_name ){ cout << " The winner is: " << winner_name << " !\n"; };

The other way a ASCIZ variable may store no characters:
The string variable may contain the address of a valid memory location that contains no other characters before the end-of-string NUL character.
Some programs have many such locations in memory, located at a variety of addresses (that address is stored in the string variable), that contain such a "null string" or "zero-length string".
For example,
char buffer[BUFFER_SIZE] = "";
or equivalently
char buffer[BUFFER_SIZE] = {'\0'};

Sometimes programmers can write the code that handles normal strings in such a way that that code also handles zero-length strings, with no explicit check for a "zero-length string" possibility.