OA. free
Free
Qualcomm Embedded Systems & Hardware Core Computer Science Medium

What will be the output of the program given below?

Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.

What will be the output of the program given below?

#include <stdio.h>
#include <stdlib.h>

#define MAXROW 3
#define MAXCOL 4

int main()
{
    int (*p)[MAXCOL] [MAXROW];
    p = (int (*)[MAXCOL]) malloc (sizeof(*p));
    printf("%u", sizeof(*p));
}

**MCQ

Choose one option.
Show answer & explanation
Answer: B. 48

The pointer p is declared as int (*p)[MAXCOL][MAXROW], which is a pointer to a 2D array with dimensions [MAXCOL][MAXROW]. When dereferenced with *p, it yields the full 2D array. The size of this array is MAXCOL * MAXROW * sizeof(int) = 4 * 3 * 4 = 48 bytes (assuming sizeof(int) = 4). The sizeof(*p) evaluates to 48.

Step-by-step Derivation:
Step-by-step:

  1. p is declared as: int (*p)[MAXCOL][MAXROW] — a pointer to a 2D array.
  2. The type of *p is int [MAXCOL][MAXROW] — a 2D array with 4 rows and 3 columns.
  3. sizeof(*p) = number of elements × sizeof(int) = (MAXCOL × MAXROW) × sizeof(int) = (4 × 3) × 4 = 12 × 4 = 48 bytes.
  4. printf("%u", sizeof(*p)) outputs: 48