How does & x change from x if x is a pointer?

So ... I have the following code:

int main(void)
{
const char *s="hello, world";
cout<<&s[7]<<endl;

return 0;
}

and he prints "world" ... however, I do not understand why this is:

int main(void)
{
const char *s="hello, world";
cout<<s[7]<<endl;

return 0;
}

it will only print โ€œwโ€ (everything that I changed got rid of the ampersand), but I thought it was the โ€œaddress of the operatorโ€ ... which makes me wonder why you need it and what kind of function it is?

+3
source share
3 answers

s[7] - 'w', &s[7] 'w'. char* cout, , , , , 'w' , \0. world.

,

const char *s="hello, world";
const char  *pchar = &s[7]; //initialize pchar with the address of `w`
cout<< pchar <<endl; //prints all chars till it finds `\0` 

:

world

, s[7], &s[7], :

cout << ((void*)&s[7]) << endl; //it prints the address of s[7]

void*, cout , char*. void* . cout . , , .

-: http://www.ideone.com/BMhFy

+10

... .

s char ( C), s[7] char. s[7] char, &s[7] char ( C).

+4

s [7] is the symbol. (You can index pointers to an array as if it were just the name of the array).

& s [7] is a pointer to a character in index 7. it is of type char *, so the stream input tool treats it as char *.

0
source

All Articles