Last active
May 10, 2021 23:26
-
-
Save itmir913/79e51d94b81f3fa084e243252863ee22 to your computer and use it in GitHub Desktop.
[2차원 배열 동적할당] C언어 malloc 함수로 2차원 배열 동적할당하기 #배열 #동적할당
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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