On my Debian/Sid/x86-64 the below program (in source file itsols.cc
compiled with the command g++ -Wall itsols.cc -o itsols
)
#include <iostream>
#include <string>
int main(int, char**) {
std::string s = "Hello";
std::cout << s[3] << std::endl;
return 0;
}
displays a single lower-case l
(compiled with g++ -Wall itsols.cc -o itsols
, both with GCC 4.7.2 and with GCC 4.8.0 and also with GCC 4.6.3)
So I cannot reproduce what you claim (and I believe you are probably wrong, because when you read C++ reference material the operator [] on strings gives a const char&
result, which is output using std::cout <<
as a single char, not a number).
addenda
Even by taking your [final] example (which is not correct C++ because main
has a bad signature, and GCC warns about that) I am getting two A
outputs (and this using either GCC 4.6.3, or GC 4.7.2, or GCC 4.8.0). Also, your example lacks a std::endl
and a final putchar('\n'); return 0;
….
Take the habit to always pass -Wall
to g++
or gcc
(also passing -g
while you debug your program with gdb
, later optimizing it with -O2
when there is no bugs), and also check with g++ -v
that you are running the compiler you believe, and to get its version.
4
solved In C++ why does reference to single character of std::string give a number [closed]