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;
}
Discussion:
38 comments Page 2 of 4.
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.
Please clarify me about arrays arithmetic operations. Give me reply as soon as possible.
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?
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?
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.
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?
Why are we using that static here? What can it do?
Without static in that line what will happen?
Can anyone answer me?
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.
We can write a[1][-1] as *(*(100+4)-2).it will evaluate to *102 which is nothing but the value 6.
Neeraj said:
1 decade ago
@anand, @vedavati, @karthiga
If you urself can't understand then don't try to explain the answer here, it will confuse others, thank you.
If you urself can't understand then don't try to explain the answer here, it will confuse others, thank you.
Khurram Baig said:
1 decade ago
4th step is explained wrongly.
++*ptr mean value pointed by ptr is being incremented. So it increments arr+3 or arr[2].
++*ptr mean value pointed by ptr is being incremented. So it increments arr+3 or arr[2].
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.
So the last printf prints 3 4 4.
Sweety said:
7 years ago
Hi, I didn't get it. Could you guys make it as simple as possible and explain?
HARI said:
9 years ago
How ptr points to 0? It should point to the address of first element in p.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers