Discussion
What will be the output of the following C code?
#include <stdio.h>
void foo(int*);
int main()
{
int i = 10;
foo((&i)++);
}
void foo(int *p)
{
printf("%d\n", *p);
}
Answer: C
Correct answer: Compile time error
&i yields the address of i, but it is a value, not an object - there is no storage holding it that could be incremented.
++ requires a modifiable lvalue, so (&i)++ is a constraint violation and the compiler rejects it. The contrast with the next case is instructive: if the address is first stored in a pointer variable, that variable is an object and can be incremented.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development