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 4 of 4.

Naresh said:   1 decade ago
++*ptr; this statement changes the p[3], to points to arr[4].

So the last printf prints 3 4 4.

Kushal said:   1 decade ago
I can't understand the last row. Why's it 3 4 4?

Saurabh tiwari said:   1 decade ago
Please give me solid answer with fully explanation.

Karthiga said:   1 decade ago
p[]=(0,1,2,3,4);
ptr is point to pointer operation which will increament index position by one.

So , Now its point to 1. So ptr 1 and p 0

So for first printf statement ptr-p, *ptr-arr, **ptr => 1-0,1-0,1

Same way fellow for next by increment ptr where *ptr and ptr give the value in the index postion.

Sivakumar.m said:   1 decade ago
Please explain any one.

Rahul said:   1 decade ago
Please anyone explain this in detail.

Vedavathi said:   2 decades ago
Here we will identify the answer depending on **ptr. At first **ptr points to first element i.e '0'. After incrementind ptr++ that address 2nd element. So in the options 'C' option have '1'. So we guess 'c' as the answer.

Anand shiva said:   2 decades ago
Please give me a brief description.


Post your comments here:

Your comments will be displayed after verification.