strings are a little special. a pointer to characters is a C style string and cout knows to print every letter in the array until it hits the zero character.
this is the exact same as
cout << names[2]
which is the more normal way to write that.
This looks a lot like a question to me. Anyway, when you send a char* into std::cout, std::cout assumes it's a C string and prints that. Note, this is a lot easier if you used C++ strings:
I understand that names[2] is a more normal way to write it and strings are a pointer to its first element but how is &names[2][0] correct in printing the name ?
There's a challenge with the blanks, maybe < 10 characters
Size: 60 i.e. 6 x 10
1: raman ** r
2: 0x7fff5fbff764 ** raman
t before: = r ... t after: = r
t before: = a ... t after: = a
t before: = m ... t after: = m
t before: = a ... t after: = a
t before: = n ... t after: = n
t before: = ... t after: =
t before: = ... t after: =
t before: = ... t after: =
t before: = ... t after: =
t before: = ... t after: =
srinivas ** raman
Program ended with exit code: 0
I understand names[2] and names[3] but don't understand &names[2][0]
ok.
names[2] is basically a pointer where the first letter of the 'string' data is stored.
names[2][0] IS the first letter of this string. if you print it, you will get 'r' from raman
&names[2][0] is the address of 'r' which is a pointer to the first letter of the string... this is IDENTICAL to names[2]. The address of operation effectively cancels out the [0] operation. Note that [0] is sort of special in that it is the starting point. In pointer-speak, that is (names[2])+0 ... this is important, though the zero "does nothing" here. (this is not entirely true in terms of memory layout because strings have other internal data, this is conceptual, as if strings were just a char* .. to help you understand, think this way for a moment)
&names[2][1] is the address of the letter 'a' in "raman"
and printing THAT would show "aman" ... you skipped the r. Now back to pointer-speak: this is (names[2])+1 !!! (again, just conceptually)