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  Bitçiye nasıl üye olunur?

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  How many mountain climbers can the average person do?

Leave a Reply

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