Doubt in character array

Output of the below code is
1
2
raman srinivas
srinivas raman

What is the logic behind the output of &names[2][0] being raman ? Shouldn't &names[2][0] print the address ?Please explain

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;
int main( ) 
{ 
char names[ ][10] = {"akshay", "parag", "raman", "srinivas", "gopal", "rajesh" } ; 

int i ; 
char t ; 
cout<<&names[2][0]<<" "<<&names[3][0]<<endl ; 
for ( i = 0 ; i <= 9 ; i++ ) 
{ 
t = names[2][i] ; 
names[2][i] = names[3][i] ; 
names[3][i] = t ; 
} 
cout<<&names[2][0]<<" "<<&names[3][0]  ; 
} 
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
#include <utility>
int main( ) 
{ 
  std::string names[] = {"akshay", "parag", "raman", "srinivas", "gopal", "rajesh"}; 
  std::cout << names[2] << ' ' << names[3] << '\n'; 
  std::swap(names[2], names[3]);
  std::cout << names[2] << ' ' << names[3] << '\n'; 
} 
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 ?
I understand names[2] and names[3] but don't understand &names[2][0]
closed account (48T7M4Gy)
Hint: Sometimes in addition to the above if you experiment and print things out you get a lot out of it to show what's going on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>

using namespace std;

int main( )
{
    char names[ ][10] =
    {
        "akshay", // char[0]
        "parag",
        "raman",
        "srinivas",
        "gopal",
        "rajesh" // char[5]
    } ;
    
    int i = 0;
    char t;
    
    size_t size = sizeof(names)/sizeof(char);
    
    cout << "Size: " << size << " i.e. 6 x 10\n";
    
    
    cout << "1: " << names[2] << " ** " << names[2][0] << endl;
    cout << "2: " << &names[2] << " ** " << &names[2][0] << endl;
    
    for ( i = 0 ; i <= 9 ; i++ )
    {
        t = names[2][i];
        cout << "t before: = " << t;
        names[2][i] = names[3][i] ;
        names[3][i] = t ;
        cout << " ... t after: = " << t << '\n';
    }
    cout<< &names[2][0] << " ** " << &names[3][0] << '\n';
}


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
Last edited on
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)
Understood. Thanks all
Topic archived. No new replies allowed.