Open links in new tab
    • Work Report
    • Email
    • Rewrite
    • Speech
    • Title Generator
    • Smart Reply
    • Poem
    • Essay
    • Joke
    • Instagram Post
    • X Post
    • Facebook Post
    • Story
    • Cover Letter
    • Resume
    • Job Description
    • Recommendation Letter
    • Resignation Letter
    • Invitation Letter
    • Greeting Message
    • Try more templates
  1. To dynamically allocate a 2D array and take user input in C, you can use malloc to allocate memory on the heap. Below is a step-by-step guide with an example.

    Steps to Allocate and Input a 2D Array Dynamically:

    1. Allocate Memory for Rows: Use a pointer-to-pointer (int**) to allocate memory for the rows.

    2. Allocate Memory for Columns: For each row, allocate memory for the columns using malloc.

    3. Take User Input: Use nested loops to input values into the allocated 2D array.

    4. Free Allocated Memory: After usage, free the memory row by row and finally free the pointer-to-pointer.

    Example Code:

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

    int main() {
    int rows, cols;

    // Get dimensions from user
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    printf("Enter number of columns: ");
    scanf("%d", &cols);

    // Allocate memory for rows
    int** array = (int**)malloc(rows * sizeof(int*));
    if (array == NULL) {
    printf("Memory allocation failed!\n");
    return 1;
    }

    // Allocate memory for columns in each row
    for (int i = 0; i < rows; i++) {
    array[i] = (int*)malloc(cols * sizeof(int));
    if (array[i] == NULL) {
    printf("Memory allocation failed!\n");
    return 1;
    }
    }

    // Take input from user
    printf("Enter elements of the 2D array:\n");
    for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
    printf("Element [%d][%d]: ", i, j);
    scanf("%d", &array[i][j]);
    }
    }

    // Print the array
    printf("\nThe 2D array is:\n");
    for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
    printf("%d ", array[i][j]);
    }
    printf("\n");
    }

    // Free allocated memory
    for (int i = 0; i < rows; i++) {
    free(array[i]);
    }
    free(array);

    return 0;
    }
    Copied!
    Feedback
  2. How to dynamically allocate a 2D array in C? - GeeksforGeeks

    Jan 10, 2025 · We can create an array of pointers of size r. Note that from C99, C language allows variable sized arrays. After creating an array of pointers, we can dynamically allocate memory for …

  3. Malloc a 2D array in C - Stack Overflow

    Apr 27, 2016 · It is just that there are too many programmers who think there are no multi-dimensional arrays in C. But just beause "we did it that way all the time" does not mean it is the correct way.

    • Reviews: 3

      Code sample

      array = malloc(sizeof(*array) * ROWS); // COLS is in the `sizeof`
      array = malloc(sizeof(int[ROWS][COLS])); // explicit 2D array notation
    • People also ask
    • 9.3. Dynamic Memory Allocation of 2D Arrays - Learning C

      The first method to dynamically allocate a 2D array is to allocate an array of pointers, and then have each of these pointers point to a dynamically allocated …

    • How to Create a 2D Array Dynamically in C Using malloc ()

      Dec 27, 2023 · In this comprehensive guide, you‘ll learn how to create 2 dimensional (2D) arrays dynamically in C using malloc (). We‘ll cover the fundamentals of 2D arrays, reasons to use malloc (), …

    • Dynamically Allocating 2D Arrays Efficiently (and …

      Jun 27, 2023 · The quirky interplay between arrays and pointers in C allows …

    • C Program to Create a 2D Array Using malloc ()

      Sep 24, 2025 · In this tutorial, we will show how to allocate memory for a 2D array using pointers and malloc(), input and output values, and safely release memory …

    • How to dynamically allocate a 2D array in C? - Online Tutorials Library

      How to dynamically allocate a 2D array in C? A 2D array can be dynamically allocated in C using a single pointer. This means that a memory block of size row*column*dataTypeSize is allocated using malloc …

    • Dynamic Array in C - GeeksforGeeks

      Jul 23, 2025 · To solve this problem, dynamic arrays come into the picture. A Dynamic Array is allocated memory at runtime and its size can be changed later …

    • Dynamically Allocate a 2D Array in C - Naukri Code 360

      Aug 13, 2025 · This blog discusses the theoretical and practical implementation of how to dynamically allocate a 2D array in C.

    • Dynamic Allocation of Array in C: Techniques & Examples

      Aug 10, 2023 · Dynamic array allocation allows creating arrays at runtime with the help of functions like `malloc` or `calloc`, which enable resizing and provide more flexibility in memory usage.