Consider the following C program: #include <stdio.h> struct student { int roll; int cgpa; int sgpa[8]; }; int main() { struct student s = { 12, 8, 7, 2, 5, 9 }; int *ptr; ptr = (int *)&s; printf("%d", *(ptr + 3)); return 0; } What value is printed by this program on a typical system where int values are stored consecutively without padding between struct members?

Difficulty: Medium

Correct Answer: 2

Explanation:


Introduction / Context:
This question examines how structures are laid out in memory in C, how initializer lists map to struct members and array elements, and how pointer arithmetic over a struct can be used to access low level integer values. It also checks whether you can translate a struct with an array member into its underlying sequence of integers in memory.


Given Data / Assumptions:

    The struct student has two int members roll and cgpa, followed by an array sgpa of 8 int elements.
    The initialization struct student s = {12, 8, 7, 2, 5, 9}; assigns 12 to roll, 8 to cgpa, and the remaining values to sgpa[0], sgpa[1], sgpa[2] and sgpa[3]; the rest of sgpa is filled with zeros.
    The pointer ptr is of type int * and is assigned the address of the struct object s via a cast.
    The program prints *(ptr + 3), which refers to the fourth int in the contiguous block representing the struct.


Concept / Approach:
In C, the members of a struct are laid out in memory in declaration order, subject to possible padding rules. In this question, we assume that there is no unexpected padding between these int members, so the struct can be treated as a flat sequence of int values: roll, cgpa and then the sgpa elements. Pointer arithmetic on ptr moves in units of sizeof(int), so ptr + 3 refers to the fourth integer in this sequence.


Step-by-Step Solution:
Determine the mapping of initializer values: roll = 12, cgpa = 8. The next initializer values 7, 2, 5 and 9 are assigned to sgpa[0], sgpa[1], sgpa[2] and sgpa[3] respectively. Remaining sgpa elements become 0. The flat sequence of int values in memory is therefore: [12, 8, 7, 2, 5, 9, 0, 0, 0, 0]. ptr is an int pointer to the start of s, so ptr points to the first value 12. ptr + 3 points to the fourth element in the sequence, which is 2 (sgpa[1]). Therefore *(ptr + 3) is 2.


Verification / Alternative check:
You can verify this by printing out all ten ints starting from ptr using a loop and confirming their values. The output will match the sequence above. As long as the compiler does not insert padding between int members (which is a common assumption for such exam questions), the reasoning is straightforward.


Why Other Options Are Wrong:
Option a (8) corresponds to cgpa, which is the second integer in the sequence and would be at *(ptr + 1), not *(ptr + 3).
Option b (7) is sgpa[0], which is the third integer and would be at *(ptr + 2).
Option d suggests a compiler error, but the struct initialization is valid and legal C. The cast from &s to int * is not ideal style but is allowed.


Common Pitfalls:
Learners sometimes miscount the positions in the initializer list or forget that arrays inside structs are stored inline as consecutive elements. Another pitfall is assuming that pointer arithmetic on ptr counts bytes rather than elements; in C, adding 1 to an int pointer moves by sizeof(int) bytes, not 1 byte, which automatically aligns with the next integer.


Final Answer:
The expression *(ptr + 3) accesses the fourth integer in the struct's memory layout, which has the value 2.

More Questions from Programming

Discussion & Comments

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