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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice