How dynamically allocate memory for 2d array in C?

int row = 2, col = 3; int *arr = (int *)malloc(row * col * sizeof(int)); int i, j; for (i = 0; i < row; i++) for (j = 0; j < col; j++) *(arr + i*col + j) = i + j; Then the values of the 2-D array are displayed. Finally the dynamically allocated memory is freed using free. The code snippet that shows this is as follows.

Sizin İçin Seçtik  Does C support dynamic memory allocation?

Leave a Reply

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

How dynamically allocate memory for 2d array in C?

int row = 2, col = 3; int *arr = (int *)malloc(row * col * sizeof(int)); int i, j; for (i = 0; i < row; i++) for (j = 0; j < col; j++) *(arr + i*col + j) = i + j; Then the values of the 2-D array are displayed. Finally the dynamically allocated memory is freed using free. The code snippet that shows this is as follows.

Sizin İçin Seçtik  In which segment does dynamic memory allocation takes place?

Leave a Reply

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