Skip to content

Instantly share code, notes, and snippets.

@itmir913
Last active May 10, 2021 23:26
Show Gist options
  • Select an option

  • Save itmir913/79e51d94b81f3fa084e243252863ee22 to your computer and use it in GitHub Desktop.

Select an option

Save itmir913/79e51d94b81f3fa084e243252863ee22 to your computer and use it in GitHub Desktop.
[2차원 배열 동적할당] C언어 malloc 함수로 2차원 배열 동적할당하기 #배열 #동적할당
#include <stdio.h>
#include <stdlib.h>
int **createMatrix(int height, int width) { // https://codeng.tistory.com/8
int **arr;
arr = (int **) malloc(sizeof(int *) * height);
arr[0] = (int *) malloc(sizeof(int) * width * height);
for (int i = 1; i < height; i++) {
arr[i] = arr[i - 1] + width;
}
return arr;
}
int main(void) {
int **aa = createMatrix(10, 10);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
aa[i][j] = 0;
printf("%d ", aa[i][j]);
}
printf("\n");
}
free(aa[0]);
free(aa);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment