Multiple choice technology

How can I dynamically allocate a two-dimensional array?

  1. int *array1 = (int *)malloc(nrows * sizeof(int *));

  2. int *array2 = (int *)malloc(nrows * sizeof(int *));

  3. int *array3 = (int *)malloc(nrows * ncolumns * sizeof(int));

  4. Any of the above.

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

All three methods are valid approaches to allocate 2D arrays dynamically. Option A allocates an array of pointers then each row separately (pointer-to-pointer). Option B appears identical to A in the garbled text but represents the same approach. Option C allocates a single contiguous block and uses arithmetic to index (flattened 2D array). Each has trade-offs in syntax and memory layout.