ExamVeda
Login
Home
This question belongs to C Program Pointer
Pointer
?

What would be the output for the following Turbo C code?
#include<stdio.h>
void main()
{
   int a[]={ 1, 2, 3, 4, 5 }, *p;
   p=a;
   ++*p;
   printf("%d ", *p);
   p += 2;
   printf("%d", *p);
}

Answer & Solution
Correct Answer: Option D
Let's discuss the solution. Join the discussion
Examveda
Question posted by Examveda
Community

Join the Discussion

5 Comments
Ananny Joshi
Ananny Joshi 6 years ago
It is printing 3 because is as follows :-
p = a; //address of a is stored in p
*p++ //incrementing the value inside that address (not the address).
//(*p = *p+1) //1+1 = 2
printf(*p) // 2
Now,
p = p + 2
//here p contains the address not the value inside that address so (p = a)where a was at 0 location
p = 0+2 = 2 its a location so
it will print a[2]
printf(*p)//we are value of p at location 2 thats why it will print 3.
op:-(2,3)
Sharvari Patil
Sharvari Patil 7 years ago
here firstly the value of p* is 2 and value of p is the value of a
so value of p is 1 and p* is 2
now p=p+2
hence p=1+2=3
hence the ans is 2 and 3
Chandra Bharghavi
Chandra Bharghavi 7 years ago
here *p is different from p .so, at first we had incremented p*, then we had incremented p.so when we print p* variable at 1st time it gives out put as 2 and 3.
Prudhvi Suchendra
Prudhvi Suchendra 8 years ago
The solution is :
2 3
not
3 3,
because ++*p is interpreted as ++(*p) and increments value of a[0].
Darshan Bodige
Darshan Bodige 10 years ago
please explain this problem in a more effective way!
couldn't understand the solution