1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Why data() appends a NULL character at the end of string?

Discussion in 'C++' started by LarryChen, Jun 23, 2010.

  1. LarryChen

    LarryChen New Member

    Joined:
    Jun 23, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    As we know member function data() defined by string won't append a NULL character at the end of string. But I found it still appends a NULL character at the end of string. Please check the following code,
    Code:
    int main() 
    {
    	string s = "Mike";
    
    	char* q = const_cast<char*>(s.data());
    
    	while(*q)
    	{
    		q++;
    	}
    
    	return 0;
    }
    
    Debug this piece of code, you will find out there is a NULL character at the end of string. Why?
     

Share This Page