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.

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

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

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

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

Raji said:   8 years ago
Thank you @Leenaja.

Vishwa said:   8 years ago
#include<stdio.h>
int main()
{
int a[][2]={3,6,8,9};
printf("%d\n",a[1][-1]);
return 0;
}
I got output 6.

Can anyone explain this?

Neeraj said:   8 years ago
What if the array arr is not declared as static?

Rohit said:   8 years ago
I have considered 100 as the base address.

We can write a[1][-1] as *(*(100+4)-2).it will evaluate to *102 which is nothing but the value 6.

Sweety said:   7 years ago
Hi, I didn't get it. Could you guys make it as simple as possible and explain?

Archana said:   7 years ago
What about that 'static' ?
Why are we using that static here? What can it do?
Without static in that line what will happen?

Can anyone answer me?

Karthick said:   1 decade ago
Step 4 confused please help.


Post your comments here:

Your comments will be displayed after verification.