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  Ev aldım tapu masrafı ne kadar?

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  What is malloc and free in C?

Leave a Reply

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