Skip to main content
edited body
Source Link
Charles Salvia
  • 7.4k
  • 1
  • 37
  • 33

Short answer: a null terminated string is a char array with a null value (0x00) after the last valid character in the string.


Long Answer:

It's important to remember that not every C and C++ compiler will initialize values for you. AFAIK, most don't.

A basic string in C or C++ (without STL) is simply an array of characters.

char myString[25];

At this point in time, we have no idea what's within that string. It could be empty; it could have garbage characters (most likely); or it could have meaningful information. It all depends upon what was in that memory segment before the array was declared.

Note that we have 24 characters of storage here, and the null will take the 25th character.

It's common practice to pre-fill and clear a string with nulls to get rid of any garbage.

memset(0x00myString, myString0x00, 25);

Note that in this case I'm using a hexadecimal declaration of 0 to indicate NULL. Some compilers and / or libraries have a NULL value or similar defined.

Many of the basic string functions like strcmp, strcat, etc... rely upon null terminated strings to indicate the end of the string. If you don't have it terminated then the string function can run off the end of the string and not act as you would expect.

The C++ STL string is an actual object and takes care of some of those initialization / termination concerns for you.

Short answer: a null terminated string is a char array with a null value (0x00) after the last valid character in the string.


Long Answer:

It's important to remember that not every C and C++ compiler will initialize values for you. AFAIK, most don't.

A basic string in C or C++ (without STL) is simply an array of characters.

char myString[25];

At this point in time, we have no idea what's within that string. It could be empty; it could have garbage characters (most likely); or it could have meaningful information. It all depends upon what was in that memory segment before the array was declared.

Note that we have 24 characters of storage here, and the null will take the 25th character.

It's common practice to pre-fill and clear a string with nulls to get rid of any garbage.

memset(0x00, myString, 25);

Note that in this case I'm using a hexadecimal declaration of 0 to indicate NULL. Some compilers and / or libraries have a NULL value or similar defined.

Many of the basic string functions like strcmp, strcat, etc... rely upon null terminated strings to indicate the end of the string. If you don't have it terminated then the string function can run off the end of the string and not act as you would expect.

The C++ STL string is an actual object and takes care of some of those initialization / termination concerns for you.

Short answer: a null terminated string is a char array with a null value (0x00) after the last valid character in the string.


Long Answer:

It's important to remember that not every C and C++ compiler will initialize values for you. AFAIK, most don't.

A basic string in C or C++ (without STL) is simply an array of characters.

char myString[25];

At this point in time, we have no idea what's within that string. It could be empty; it could have garbage characters (most likely); or it could have meaningful information. It all depends upon what was in that memory segment before the array was declared.

Note that we have 24 characters of storage here, and the null will take the 25th character.

It's common practice to pre-fill and clear a string with nulls to get rid of any garbage.

memset(myString, 0x00, 25);

Note that in this case I'm using a hexadecimal declaration of 0 to indicate NULL. Some compilers and / or libraries have a NULL value or similar defined.

Many of the basic string functions like strcmp, strcat, etc... rely upon null terminated strings to indicate the end of the string. If you don't have it terminated then the string function can run off the end of the string and not act as you would expect.

The C++ STL string is an actual object and takes care of some of those initialization / termination concerns for you.

clarified answer
Source Link
user53019
user53019

NotShort answer: a null terminated string is a char array with a null value (0x00) after the last valid character in the string.


Long Answer:

It's important to remember that not every C and C++ compiler will initialize values for you. AFAIK, most don't.

A basic string in C or C++ (pre-STLwithout STL) is simply an array of characters.

char myString[25];

At this point in time, we have no idea what's within that string. It could be empty; it could have garbage characters (most likely); or it could have meaningful information. It all depends upon what was in that memory segment before the array was declared.

Note that we have 24 characters of storage here, and the null will take the 25th character.

It's common practice to pre-fill and clear a string with nulls to get rid of any garbage.

memset(0x00, myString, 25);

Note that in this case I'm using a hexadecimal declaration of 0 to indicate NULL. Some compilers and / or libraries have a NULL value or similar defined.

Many of the basic string functions like strcmp, strcat, etc... rely upon null terminated strings to indicate the end of the string. If you don't have it terminated then the string function can run off the end of the string and not act as you would expect.

The C++ STL string is an actual object and takes care of some of those initialization / termination concerns for you.

Not every C and C++ compiler will initialize values for you.

A basic string in C or C++ (pre-STL) is simply an array of characters.

char myString[25];

At this point in time, we have no idea what's within that string. It could be empty; it could have garbage characters (most likely); or it could have meaningful information. It all depends upon what was in that memory segment before the array was declared.

Note that we have 24 characters of storage here, and the null will take the 25th character.

It's common practice to pre-fill and clear a string with nulls to get rid of any garbage.

memset(0x00, myString, 25);

Note that in this case I'm using a hexadecimal declaration of 0 to indicate NULL. Some compilers and / or libraries have a NULL value or similar defined.

Many of the basic string functions like strcmp, strcat, etc... rely upon null terminated strings to indicate the end of the string. If you don't have it terminated then the string function can run off the end of the string and not act as you would expect.

The C++ STL string is an actual object and takes care of some of those initialization / termination concerns for you.

Short answer: a null terminated string is a char array with a null value (0x00) after the last valid character in the string.


Long Answer:

It's important to remember that not every C and C++ compiler will initialize values for you. AFAIK, most don't.

A basic string in C or C++ (without STL) is simply an array of characters.

char myString[25];

At this point in time, we have no idea what's within that string. It could be empty; it could have garbage characters (most likely); or it could have meaningful information. It all depends upon what was in that memory segment before the array was declared.

Note that we have 24 characters of storage here, and the null will take the 25th character.

It's common practice to pre-fill and clear a string with nulls to get rid of any garbage.

memset(0x00, myString, 25);

Note that in this case I'm using a hexadecimal declaration of 0 to indicate NULL. Some compilers and / or libraries have a NULL value or similar defined.

Many of the basic string functions like strcmp, strcat, etc... rely upon null terminated strings to indicate the end of the string. If you don't have it terminated then the string function can run off the end of the string and not act as you would expect.

The C++ STL string is an actual object and takes care of some of those initialization / termination concerns for you.

Source Link
user53019
user53019

Not every C and C++ compiler will initialize values for you.

A basic string in C or C++ (pre-STL) is simply an array of characters.

char myString[25];

At this point in time, we have no idea what's within that string. It could be empty; it could have garbage characters (most likely); or it could have meaningful information. It all depends upon what was in that memory segment before the array was declared.

Note that we have 24 characters of storage here, and the null will take the 25th character.

It's common practice to pre-fill and clear a string with nulls to get rid of any garbage.

memset(0x00, myString, 25);

Note that in this case I'm using a hexadecimal declaration of 0 to indicate NULL. Some compilers and / or libraries have a NULL value or similar defined.

Many of the basic string functions like strcmp, strcat, etc... rely upon null terminated strings to indicate the end of the string. If you don't have it terminated then the string function can run off the end of the string and not act as you would expect.

The C++ STL string is an actual object and takes care of some of those initialization / termination concerns for you.