#include <cstdlib>

using namespace std;

int main()
     char s[5];
     string g;
     int p=345;





     return 0;


I have a very small doubt about strings. this is code snippet i have shown. when i have called sprintf(), then it has changed 345 to string with NULL character at this end. right ? so when i equalize g with s, then it is also = to 345. but qstn is that why string is rejecting NULL at the end ? and what can i do if i want to have a NULL in the g when i am writing "g=s". please help me if u can. thanks. (i am just learning all these things, not a experieced C++ programmer).

Recommended Answers

All 12 Replies

but qstn is that why string is rejecting NULL at the end ?

What does that mean? What do you think the string object g should contain after this: g=s;? What do you mean by saying it is rejecting NULL?

I will explain what happens, and you can tell me where it doesn't do what you think it should.

s is a char pointer that points at an array that now contains the chars: '3' '4' '5' NULL

g is a string object. When you use the = operator on a string object with a char-pointer, as in this case, the characters that the char pointer is pointing to get copied into the character storage area of the string object, with a NULL at the end. So now, the string object g contains somewhere in it the chars: '3' '4' '5' NULL

On a string object, when you use the += operator with a string literal (which is what ".mp3" is) a function is run. The function is the string objects += function (better called an operator, but you can think of it like a function). The function takes the letters of the string literal, and writes them into the character storage area, starting at the NULL (which is overwritten), and then puts a new NULL on the end. So now the string object's character storage area contains the following: '3' '4' '5' '.' 'm' 'p' '3' NULL

What in that is unexpected to you? Do not think that the = and += operators simply jam characters on the end of NULLs. That's not what they do, and they are different for every kind of object you can use them on. It is up to you to read the documentation for objects you are using to learn what they do.

yes, u r correct. but when i print the g.size() after g=s. it gives me 3 and when i do g+='\0'; and then print g.size() it give me 4. but when i write g=s and s already have NULL with it, then why is it giving me 3 rather than 4 ?(because there are 4 charcters in strins g). i hope i am clear in my doubt. thanks.

NULL here, is also called the "end of string" character.

yes, i know it is equal to number of characters. but when i write g=s and when i write g+='\0'; it give me 3 and 4 respectively. why does it happen.

secondly, i am saying it is rejecting null when i write g=s because again i am appeneding some string with it and it still take it. that mean it dont have null at the end as usual, that y string got append with it, otherwise it will not appened. and that y i have asked why it has rejetced null in g=s ? doesn't the size() function stops when i get NULL character ? thanks.

I was not clear enough. Here is the part of g, the string object, that holds the characaters (the string object is a proper C++ object with lots more inside it, but we only care about the characters is holds at the moment).

'1' '2' '3' '\0'

What is the size? The size is three, because the size is the number of characters excluding the final NULL.

Now add another '\0' to the string. Now it looks like this:

'1' '2' '3' '\0' '\0'

What is the size? The size is four, because the size is the number of characters excluding the final NULL.

Now add another '\0' to the string. Now it looks like this:

'1' '2' '3' '\0' '\0' '\0'

What is the size? The size is five, because the size is the number of characters excluding the final NULL.

doesn't the size() function stops when i get NULL character ?

No. A C++ string object is a proper object that knows how big it is. It does not simply tell you how many characters it has until the first NULL. Read the documentation I pointed you at above.

commented: nicely answered!! +2

yes, now you are very clear. but one more question, i have read somewhere that string (c++) don't have null at the end. and if you want to have null at the end , then use it as g.c_str(), then you will null at the end like in C array. can you throw somwe light on that ? thanks a lot.

i have read somewhere that string (c++) don't have null at the end

"string (c++)" is an object. It's meaningless to ask if it has a NULL at the end. It's a C++ object. It has functions and data and all that sort of thing. "NULL at the end of a proper C++ object" doesn't make any sense.

A C++ string object contains in it somewhere a pointer to an array of char. That array will probably have a NULL at the end (if the coder who wrote that string class made it easy on themself), but it doesn't have to. It can also have one in the middle. It can have more than one. You can put whatever you like in it.

The c_str() function gives you a pointer to character data with a NULL on the end, which is guaranteed to be the same character data as inside the string. Usually, whoever wrote the string class object will have made it easy and you get a pointer to the actual data in the string class with a NULL on the end (so, often the C++ string object character array does have a NULL on the end, but I think that the standard does not insist on it, but what you get by using c_str() WILL have a NULL on the end).

The way you can answer all your questions really is to get the C++ standard and read the string class documentation.

string types keep track of the length of the data internally - adding the NUL byte is not required, and is actually appended to the internal buffer, incrementing the length. Don't worry about it, and don't try to do the string class' work for it... :-) As mentioned, if you want a properly NUL-terminated C-style string from a C++ string object, then use the c_str() method to get it. Internally, actually, the buffer IS NUL terminated (usually - not required), but the NUL is not part of the length count. As said, adding a NUL byte specifically to the string WILL increment the length AND it will add another NUL byte to the buffer, making it pretty useless for most stuff.

Just to make things crystal clear, NULL is a macro intended for use as a null pointer, it should never be used as the null character ('\0', also known as NUL with one L). While in C++ NULL is defined as 0 and that's compatible with the null character, it will confuse the bejeebus out of readers of your code.

So to summarize best practice:

  • In pointer context use nullptr. Prior to C++11 use either NULL or 0.
  • In character context use '\0'. 0 is acceptable, but not recommended.
  • In integer context use 0.

sorry, if i making it chaotic, but i want to ask one thing, when i write g=s, then s has 3,4,5,NULL and then why dont string g do it like this, 3,4,5,NULL,NULL(last NULL is the NULL which class object itself provides which you said is kept by class which is of no use). thanks. P.S i am thinking that i am irriating u nothing else. :( sorry!

Ultimately, because that's not what it has been programmed to do. If you read the standard, you will see that setting a C++ string by using the = operator with a char pointer sets the internal values of the string to be the same as the contents of the char array the pointer pointed at, excluding the NULL. That's what it does.

Philosophically, this is because the data you care about is not the NULL at the end. A NULL at the end of a char array to indicate the end of the data you care about is just an ugly, useful hack from C.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.