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 1 of 4.
Anand shiva said:
2 decades ago
Please give me a brief description.
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.
Rahul said:
1 decade ago
Please anyone explain this in detail.
Sivakumar.m said:
1 decade ago
Please explain any one.
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.
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.
Saurabh tiwari said:
1 decade ago
Please give me solid answer with fully explanation.
Kushal said:
1 decade ago
I can't understand the last row. Why's it 3 4 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.
So the last printf prints 3 4 4.
Angel_eyez said:
1 decade ago
p is an array of pointers. ptr is a pointer to a pointer, initailly referring to p.
Step 1:
ptr++
ptr is incremented by 1, i.e. ptr now points to p+1 or (arr+1)
ptr-p=1, *ptr=arr+1, so *ptr-arr=1, **ptr=*(arr+1)=1
Step 2:
*ptr++
is *(ptr++), i.e. ptr=ptr+1=arr+2, the * part is not of any work here.
ptr-p=2, *ptr=arr+2, so *ptr-arr=2, **ptr=*(arr+2)=2
Step 3:
*++ptr
Similar to step 2, *++ptr=*(++ptr)
ptr-p=3, *ptr=arr+3, so *ptr-arr=3, **ptr=*(arr+3)=3
Step 4:
++*ptr
is ++(*ptr), i.e. ptr now points to arr+4, but point to be noted that *p also changes,since ptr is pointer to p. Here ptr does not change its reference position unlike all the other steps, but changes the value it is pointing to
*p[]={arr, arr+1, arr+2, arr+4, arr+4}
so, ptr-p=3, *ptr=p[3]=arr+4, so *ptr-arr=4, **ptr=*(arr+4)=4
Hope this helps
Step 1:
ptr++
ptr is incremented by 1, i.e. ptr now points to p+1 or (arr+1)
ptr-p=1, *ptr=arr+1, so *ptr-arr=1, **ptr=*(arr+1)=1
Step 2:
*ptr++
is *(ptr++), i.e. ptr=ptr+1=arr+2, the * part is not of any work here.
ptr-p=2, *ptr=arr+2, so *ptr-arr=2, **ptr=*(arr+2)=2
Step 3:
*++ptr
Similar to step 2, *++ptr=*(++ptr)
ptr-p=3, *ptr=arr+3, so *ptr-arr=3, **ptr=*(arr+3)=3
Step 4:
++*ptr
is ++(*ptr), i.e. ptr now points to arr+4, but point to be noted that *p also changes,since ptr is pointer to p. Here ptr does not change its reference position unlike all the other steps, but changes the value it is pointing to
*p[]={arr, arr+1, arr+2, arr+4, arr+4}
so, ptr-p=3, *ptr=p[3]=arr+4, so *ptr-arr=4, **ptr=*(arr+4)=4
Hope this helps
Nagesh said:
1 decade ago
I also confused man some body help.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers