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
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:
pis declared as:int (*p)[MAXCOL][MAXROW]— a pointer to a 2D array.- The type of
*pisint [MAXCOL][MAXROW]— a 2D array with 4 rows and 3 columns. sizeof(*p)= number of elements × sizeof(int) = (MAXCOL × MAXROW) × sizeof(int) = (4 × 3) × 4 = 12 × 4 = 48 bytes.printf("%u", sizeof(*p))outputs: 48