Array of Pointer

Suppose we define
const char *names[ ] = { "akshay", "parag", "raman", "srinivas", "gopal", "rajesh" } ;
In this declaration names[ ] is an array of pointers.
It contains base addresses of respective names.
That is, base address of “akshay” is stored in names[0],
base address of “parag” is stored in names[1] and so on.

Now, when we say
cout<<names[0], why does it output "akshay" and not the address of akshay ?

Also why const char is used an not char ? What is the meaning of const in the above case?
Last edited on
> why does it output "akshay" and not the address of akshay?
same reason printing a char gives you a symbol instead of its ASCII code.

> Also why const char is used an not char?
const means constant.
A string literal is constant (not mutable), and you are storing its address.
Please can you explain me in a bit more detail through an example. The names : Akshay, etc. are located somewhere else in the memory. At names[0], it is the address that is stored. Then why does it output Akshay ? Please, if possible explain through an example of code ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
   const char* names[] = { "akshay", "parag", "raman", "srinivas", "gopal", "rajesh" } ;
   const char* a_name = names[0] ;

   std::printf( "%s\n", a_name ) ; // print a_name as a null-terminated c-style string - prints the characters
   std::cout << a_name << '\n' ;   // same as above: type of 'pi' is 'pointer to const char'
                                   // calls operator<< ( const char* ) - prints the characters

   std::printf( "%p\n", a_name ) ; // print a_name as a 'pointer to void' - prints the address
   std::cout << (const void*)a_name << '\n' ; // same as above: calls operator<< ( const void* ) - prints the address

   const int n = 9 ;
   const int* pi = &n ;
   std::cout << pi << '\n' ; // type of 'pi' is not 'pointer to (const) char'
                             // calls operator<< ( const void* ) - prints the address
}
http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/
ostream has member ostream& operator<< (void* val);

but

there is also a standalone overload ostream& operator<< (ostream& os, const char* s);
http://www.cplusplus.com/reference/ostream/ostream/operator-free/

An element in your array is a const char*. It should be clear why overload
resolution selects const char* rather than void*.


The other example of JLBorges has a const int*. There is no exact match.
The compiler has three options:
1. implicit conversion from int* to void*
2. implicit conversion from int* to char*
3. report error


The tricky part is to know all the available overloads that the compiler sees after you have included standard library header(s).
http://en.cppreference.com/w/cpp/concept/FormattedOutputFunction
Thanks all. It cleared my all doubts.
> http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/
> ostream has member ostream& operator<< (void* val);

Should be const void*
See overload (7) http://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt

The const qualification is important: for instance, there is no implicit conversion from const int* to void*
Very good point. Someone has to polish the reference here. Note sent.
Topic archived. No new replies allowed.