Discussion
What is the output of this C code?
#include <stdio.h>
int x = 0;
void main()
{
int *ptr = &x;
printf("%p\n", ptr);
x++;
printf("%p\n ", ptr);
}
Answer: A
Correct answer: Same address
ptr holds the address of x, and incrementing x changes the value stored there, not where it is stored. An object does not move during its lifetime.
So both printf calls print the same pointer value. The distinction is the one beginners most often blur: x++ alters the contents of the box, ptr++ would alter which box is pointed at, and only the second changes what %p reports.
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