pointer

void main()
{
int a[]={1,2,3,4,5,6,7,8,9,10};

int *p=a;

int *q=&a[9];

cout<<q-p+1<<endl;
}

The output is 10, how?

if we give cout<<q it will print the address, value won't print....

if we give cout<<p it will print the address, value won't print....

p has the base addr; q has addr of a[9].....

q-p+1

1 100 2 102 .......10 120

so, q-p here one position moved before now it has addr of a[8]...

q-p+1 now it's again get back to the a[9]....

i expected the output as address of a[9]......

but i got the output 10...(that is value at addr )

how value will come.....

warm regards
sarwan

the output is actually difference between the address locations and in this case the it happens to be 10.
p => address of start of array
q => address of 9th element.

((q-p) + 1) ==> (9 - 0) + 1 ==> 10..

Regards