4.
Texas Instruments technical mcq question, verified with a worked answer. Free to practise - no sign-up.
Working with structures**
What output will the following code snippet produce when it is run on a 64-bit machine?
struct(
float angle, radius;
) *vector[10];
printf("%d\n", sizeof(vector));
Show answer & explanation
The code declares an array of 10 pointers to a structure. On a 64-bit machine, a pointer typically occupies 8 bytes. Therefore, the size of the array is 10 pointers * 8 bytes per pointer = 80 bytes.
Step-by-step Derivation:
Step 1: Analyze the declaration struct(...) *vector[10];. This is an array named vector containing 10 elements. Each element is a pointer (*) to a structure.
Step 2: Identify the machine architecture. The question specifies a 64-bit machine. In a 64-bit environment, the standard size for a memory pointer (address) is 8 bytes (64 bits / 8 bits per byte).
Step 3: Calculate the total size of the array. The sizeof operator applied to an array returns the total number of bytes allocated for that array.
Calculation: Total Size = (Number of elements) * (Size of each element) = 10 * 8 bytes = 80 bytes.
Step 4: Note that the internal members of the structure (float angle, radius) are irrelevant because the array stores pointers to the structures, not the structures themselves.