Dynamic 2D Array Allocation

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

int main() {
    // Dynamic 2D array allocation
    int rows, cols;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    printf("Enter the number of columns: ");
    scanf("%d", &cols);

    int **dynamic2DArray = (int **)malloc(rows * sizeof(int *));
    for (int i = 0; i < rows; i++) {
        dynamic2DArray[i] = (int *)malloc(cols * sizeof(int));
    }

    // Use the dynamically allocated 2D array

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

    return 0;
}
chevron_left
chevron_right

Leave a comment

Your email address will not be published. Required fields are marked *

Comment
Name
Email
Website