I declared two different char * and initialised them to the same string.
but i thought they would be pointing to the different addresses , but they pointed to the same addresses..
How come this happens?

char* arr = "abcdefgh";
char* brr = "abcdefgh";
cout<<arr<<endl;
cout<<brr<<endl;

the values printed out were same.. How this is happening? Plz explain

It's a compiler optimization. Given that string literals are read-only, there's no reason to create two separate instances of the same string literal in memory.

thanks..
so this is not language dependent right?
in C also i am getting same results,,,
so optimisation will be always done for string literals..
they always reside in read only memory..

so this is not language dependent right?

After a fashion. It's a language-dependent optimization, but it is very common. I would default to assuming that string literals are shared since that's a safer assumption in the general case.

in C also i am getting same results

Yes, C and C++ share roughly the same rules for string literals.

so optimisation will be always done for string literals..

It's not a guaranteed optimization, but I don't know of any modern C or C++ compilers that don't implement it.

they always reside in read only memory..

Yes, this is effectively required by both standards. A compiler can store string literals in read-write memory, but you're not allowed to take advantage of that in portable code.

This question has already been answered. Start a new discussion instead.