How can I find the address of a string literal ?
You have found the address of a string literal. The value of a string literal is the address of its first character. You assigned that value to a
and b
.
Can I say that the total memory used was sizeof (a) and not 2*sizeof (a) ?
First off the question is malformed. sizeof(a)
is the size of the pointer.
You intended to ask:
Can I say that the total memory used for the string literals is the size required for one copy of the string, and not the size required for two copies of the string?
No. That's an implementation detail of the compiler. It may choose to intern the strings or not at its whim. If you modify one of the strings, it is implementation-defined whether the other string is observed to be modified.
This is a consequence of the more general fact that if you modify one of the strings, it is implementation-defined what happens, period. Anything can happen.
my understanding of string literals is that one copy of the string is stored.
Your understanding is mistaken. That is not a guarantee of the language. That's an optimization that a compiler can choose to make or not.
Isn't it stored in between a, a+1, a+2 ..... a + 12 address in memory and also b, b + 1, b + 2 ... b + 12 in memory (12 is sizeof string)?
I cannot understand this question. What is the "it" that isn't being stored?