V
V
vladmd022021-09-10 01:11:46
C++ / C#
vladmd02, 2021-09-10 01:11:46

Does a std::string contain a null-terminated string (i.e. '\0')?

In C++ a string is a vector == pointer to start and length. But will there be a closing 0?

class String{

private:

char * str; // pointer to string

int len; // line length

static int num_strings; // number of objects

static const int CINLIM = 80; // input limit for cin
}


In other words, std::string really only contains a pointer to char, and there's no trailing null there?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Bladegreat, 2021-09-10
@vladmd02

Prior to C++11, strings may not contain a null terminator, but starting from C++11, they must. The data() method returns the same as c_str().
https://en.cppreference.com/w/cpp/string/basic_str...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question