Sunday, August 2, 2009

In c++,why should be the size of char array(to store a string) should be one larger than the no of characters?

in c++,why is it necessary to include the null character while storing a string and in c it is not necessary..why..???

In c++,why should be the size of char array(to store a string) should be one larger than the no of characters?
The NULL character is the defined string terminator in C and C++ as used by all string functions in C and the string types in the Standard Template Library (STL) and the Microsoft Foundation Classes (MFC). If you do not account for the NULL character, which practically all string functions expect you to do, you can end up with a lot of problems.





In short, it is a language characteristic that has been around since the very beginning, so it's not something to really question but just do.
Reply:The null character must be accounted for because most of the output functions using character string will stop output on detecting a null character and also string copy functions will stop copying characters upon detecting a null character.


If there was no null character, there is no way to stop the output or copy function.--deepak
Reply:The null character must be accounted for because most of the output functions using character string will stop output on detecting a null character and also string copy functions will stop copying characters upon detecting a null character.


If there was no null character, there is no way to stop the output or copy function.


No comments:

Post a Comment