# include <iostream>
using namespace std;
int main()
{
char places[3][11]={"London", "Birmingham", "Glasgow"};
char *p;
cout<<places[2]<<endl;
p=places[0];
cout<<p<<endl;
cout<< &p[2]<< endl; <------
cout<< p[11]<< endl; <-------
return 0;
}

the output is the following
Glasgow
London
ndon
B

what i dont get is how the 3rd and 4th output line came about ??
and what does &p[2] do ....
and happens with the p[11]

> Could you please post using code tags?
> Which lines are you referring to? (the third and the fourth line??)

> According of my knowledge of pointers this: &p[2] is just displaying a memory address (or am I wrong here?)

> This code is just the result of (very) bad programming :) ...

include <iostream>
using namespace std;
int main()
{
char places[3][11]={"London", "Birmingham", "Glasgow"};
char *p;
cout<<places[2]<<endl;
p=places[0];
cout<<p<<endl;
cout<< &p[2]<< endl;                 
cout<< p[11]<< endl;           
return 0;
}

the output is the following
Glasgow
London
ndon
B
what i dont get is how the 3rd and 4th output line came about ??
and what does &p[2] do ....
and happens with the p[11]

> Could you please post using code tags?
> Which lines are you referring to? (the third and the fourth line??)

> According of my knowledge of pointers this: &p[2] is just displaying a memory address (or am I wrong here?)

> This code is just the result of (very) bad programming :) ...

i dont get the 4th line
why is the output a "B"

its from a past exam paper, my knowledge isnt perfect btw hence why i'm writing here
thanks for the help :)

&p[2] is same as &(*(p+2)) which is same as p+2
So cout<<p+2 will print the array which has the the first element as the 3rd element of p. Hence the output is "ndon"

p[11] means *(p+11) that means "tell me the value of the 11th location from p" (remember p is a pointer)

Let's take a look at your code:

> You first do the following: p=places[0]; , which means that you're now pointing at the first letter of the word 'London', then somewhere further in your code you send this to cout: p[11] (but if you declare a char array like this: char places[3][11]; it can contain three elements of 10 characters long (the 11th is for the NULL-terminator: '\0' (the NULL-terminator of the string 'London' has index 10 in the array 'places')

> So what happens when you call cout << p[11] ?

>>> It's just displaying the first letter of the next element in the array of places (the 'B' of "Birmingham")

Hope this helps !

siddhantes>So cout<<p+2 will print the array...
tux>Isn't that because cout is overloaded for pointers ?

thanks alot Tux that was very helpful :)

>>Isn't that because cout is overloaded for pointers ?
cout(strictly speaking, the operator << is overloaded, not cout) is overloaded for pointer to char and assume it to be a cstring i.e. character array which are null terminated.
consider this:

char c[6]= "Hello";
int *d[5]={5,2,1,4,5};
std::cout<<c;//prints Hello
std::cout<<d;//prints the address of d[1]

>>Isn't that because cout is overloaded for pointers ?
cout(strictly speaking, the operator << is overloaded, not cout) is overloaded for pointer to char and assume it to be a cstring i.e. character array which are null terminated.

> Yeah, you're right !! Actually it is the << operator which is overloaded, thanks for the clarification siddhantes :) I'm still learning :P ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.