Hey. I'm learning about errors.
Why does this not produce an error?
I'm assuming string is an array of char. This string has 9 chars. The loop runs from 0 to 11. Will not referring to s[11] be an error?
string s = "Success!\n"; for (int i=0; i<12; ++i) cout << s[i];
Secondly, why is vector<char> v = "Hello";
an error? I get this ( g++ ) :
"conversion from ‘const char [6]’ to non-scalar type ‘Vector<char>’ requested"
What does it mean?
> Why does this not produce an error?
> This string has 9 chars. The loop runs from 0 to 11. Will not referring to s[11] be an error?
Referring to s[11] is an error when s.size() is less than 11; it is undefined behaviour.
Attempting to modify s[11] is an error when s.size() is less than 12; this too is undefined behaviour.
(Undefined behaviour need not be diagnosed.)
For bounds checked access to characters in a string, use the member function at(). http://en.cppreference.com/w/cpp/string/basic_string/at s.at(11) would throw an object of type std::out_of_range if s.size() is less than 11.