+ 1

Please explain output.

void main() { int a[]={1,2,6,4}; int *p,**q,*t; p=a; t=a+1; q=&t; cout<<*++p<<**q<<*t++; }

3rd Dec 2016, 4:24 PM
Vinay Mahore
Vinay Mahore - avatar
5 Antworten
+ 4
Before cout: ==> p points to first element of a ==> t points to second element of a ==> q points to t since it is a pointer-to-pointer At cout: ==> *++p means "increment p first and then derefence it" or simply *(p + 1). It will print the second element of a ==> since q is a pointer to a pointer, you need to dereference it twice: *q returns t pointer, **q returns what t is pointing to, which is 6. ==> *t++ also means *(t + 1) but since it's the post-increment operator it will derefence t first 'then' do the incrementing. So cout will print the second element of a which is 2. After cout: ==> t will point to the third element of a because of the t++ part. Hope this helps.
4th Dec 2016, 12:14 AM
Alper Tiryakioğlu
Alper Tiryakioğlu - avatar
+ 4
Oh, I didn't noticed it when I wrote it. You're right, how q really shows 6 when t is pointing to 2? Now I'm confused too :D
4th Dec 2016, 10:46 AM
Alper Tiryakioğlu
Alper Tiryakioğlu - avatar
+ 4
Ah, I see. So that's why. Thanks.
4th Dec 2016, 5:00 PM
Alper Tiryakioğlu
Alper Tiryakioğlu - avatar
+ 1
I got answer today as post increment operator has highest precedence thus t++ will execute first thats why q is printing 6 and t value is printing 2 bcoz compiler printing the value we asked in current statement.
4th Dec 2016, 10:52 AM
Vinay Mahore
Vinay Mahore - avatar
0
how q is pointing to 6 when t is point to 2. Not getting that point please try to explain that.
4th Dec 2016, 3:48 AM
Vinay Mahore
Vinay Mahore - avatar