2차원 배열 포인터와 매개변수 전달
2019. 7. 16. 19:05ㆍPL/C++
1. 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> | |
int main(void) { | |
int numArr[3][4] = { | |
{ 11, 22, 33, 44 }, | |
{ 55, 66, 77, 88 }, | |
{ 99, 110, 121, 132 } | |
}; | |
// int** numPtr = numArr; // 강제 캐스팅 (int**)하면 실행은 되지만, 결국 -값을 리턴하면서 끝난다 | |
int (*numPtr)[4] = numArr; // 만일 괄호를 빼먹은다면, 배열 포인터가즉 int *numPtr[4]는 포인터 배열을 의미한다 | |
printf("%d\n", numPtr[0][0]); | |
return 0; | |
} |
2. 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
#if 0 | |
반환값자료형 함수이름(자료형 매개변수[][가로크기]) | |
{ | |
} | |
반환값자료형 함수이름(자료형(*매개변수)[가로크기]) | |
{ | |
} | |
#endif | |
#include <stdio.h> | |
// 혹은 void pointerArray(int (*arr)[5], int col, int row) | |
void pointerArray(int arr[][5], int col, int row) { | |
for (int i = 0; i < row; i++) { | |
for (int j = 0; j < col; j++) { | |
printf("%d ", arr[i][j]); | |
} | |
printf("\n"); | |
} | |
} | |
int main(void) { | |
int numArr[2][5] = { | |
{ 1, 2, 3, 4, 5 }, | |
{ 6, 7, 8, 9, 10 } | |
}; | |
int col = sizeof(numArr[0]) / sizeof(int); | |
int row = sizeof(numArr) / sizeof(numArr[0]); | |
pointerArray(numArr, col, row); | |
return 0; | |
} |
'PL > C++' 카테고리의 다른 글
malloc, calloc으로 2차원 배열 임의의 크기로 다루기 (0) | 2019.07.17 |
---|---|
배열의 주소에 정수 연산 (0) | 2019.07.17 |
void *을 이용한 값 변경 (0) | 2019.07.16 |
컴퓨터 시스템이 리틀 엔디안인 경우 (0) | 2019.07.16 |
문자열 입력 getline과 stringstream으로 처리하기 (0) | 2019.06.26 |