C Programming - Arrays - Discussion

Discussion Forum : Arrays - Find Output of Program (Q.No. 5)
5.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    static int arr[] = {0, 1, 2, 3, 4};
    int *p[] = {arr, arr+1, arr+2, arr+3, arr+4};
    int **ptr=p;
    ptr++;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    *ptr++;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    *++ptr;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    ++*ptr;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    return 0;
}
0, 0, 0
1, 1, 1
2, 2, 2
3, 3, 3
1, 1, 2
2, 2, 3
3, 3, 4
4, 4, 1
1, 1, 1
2, 2, 2
3, 3, 3
3, 4, 4
0, 1, 2
1, 2, 3
2, 3, 4
3, 4, 5
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
38 comments Page 2 of 4.

Raji said:   8 years ago
Thank you @Leenaja.

Jyoti said:   8 years ago
I don't understand.

Why takes p as 0, and then 0-array as 1?

Prashant said:   9 years ago
Hi, I am not getting this. Please, anyone explain me.

HARI said:   9 years ago
How ptr points to 0? It should point to the address of first element in p.

Raji said:   9 years ago
Thank you @Angel_Eyez.

Abhishek said:   9 years ago
Thank you guys. Got it finally, special thanks to @Leenaja.

Dileep said:   9 years ago
Can't understand this? Please help me to get it.

Sri said:   10 years ago
Please give clear explanation.

Sujan said:   1 decade ago
Somewhere I read that subtraction of any pointers gives the result one (1).

Please clarify me about arrays arithmetic operations. Give me reply as soon as possible.

C.s.kumar said:   1 decade ago
Yes @Angel_Eyez is right. The above problem can be solved if we know that precedence of increment(++)operator is greater than (*) dereferencing operator.


Post your comments here:

Your comments will be displayed after verification.