Consider the following C program: #include <stdio.h> int main() { int a = 10; void *p = &a; int *ptr = p; printf("%u", *ptr); return 0; } What output does this program produce on a typical system where int is 4 bytes?

Difficulty: Easy

Correct Answer: 10

Explanation:


Introduction / Context:
This question tests your understanding of void pointers in C, how to cast them to other pointer types and what happens when you dereference them. Void pointers are generic pointers that can hold the address of any data type, but they must be cast back to a specific pointer type before dereferencing.


Given Data / Assumptions:

    An int variable a is initialized with the value 10.
    A void pointer p is assigned the address of a using p = &a;.
    An int pointer ptr is then assigned the value of p without an explicit cast, which is allowed in C but not in C++.
    The program prints *ptr using printf with the %u format specifier.


Concept / Approach:
In C, void * is a generic pointer type that can be converted to and from any other object pointer type without a cast (in C99 and later; many compilers accept it as an implicit conversion). When ptr is assigned p, ptr now points to the same location as &a. Dereferencing ptr with *ptr yields the value stored in a, which is 10. The use of %u is not ideal for printing a signed int, but on most systems the underlying bits are interpreted as an unsigned value with the same numerical representation for small positive integers.


Step-by-Step Solution:
The variable a is initialized to 10, so memory for a holds the value 10. The void pointer p receives &a, so p refers to the same address as a. The int pointer ptr is assigned p, so ptr also points to a. Dereferencing ptr with *ptr accesses the integer stored at that address, giving the value 10. printf prints this value as an unsigned integer. Since 10 is well within the range of both signed and unsigned int, it is displayed as 10 on the screen.


Verification / Alternative check:
If you run the code on a standard C compiler, you will see the output 10. Changing the format specifier to %d would also print 10. The important point is that dereferencing ptr returns the value stored in a, not its address.


Why Other Options Are Wrong:
Option b claims that the address is printed, but printf is given *ptr, which is the value, whereas printing the address would require ptr or &a in the call.
Option c suggests an arbitrary different value and has no basis in how the code operates.
Option d is incorrect for C; implicit conversion from void * to int * is allowed in C, although C++ requires an explicit cast.


Common Pitfalls:
Programmers sometimes confuse printing the content pointed to by a pointer with printing the pointer itself. Another pitfall is assuming that void pointers cannot be converted without a cast in C, which is only true in C++. When learning pointers, make sure you clearly distinguish between the address stored in the pointer and the value stored at that address.


Final Answer:
The program dereferences ptr to obtain the integer stored in a, so the output is 10.

More Questions from Programming

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion