C

[C] 118 3차원 배열을 함수에서 사용하기

qkrwngus 2021. 1. 29. 19:17

Desc :

3차원 배열을 함수에 인자로 사용하는 방법

void func( int (*x)[세로][가로] );

void func( int x[높이][세로][가로] );


Source Code :

#include <stdio.h>

void calc(int(*pscore)[100][3], int *ptotal);

void main()
{
	int score[10][100][3];
	int total[3] = { 0, };

	calc(score, total); // 배열의 주소를 인자로 넘겨준다


	printf("모든 반의 0과목 총점 : %d\n", total[0]);	// ptotal 포인터변수에서 total 값 변경
	printf("모든 반의 1과목 총점 : %d\n", total[1]);
	printf("모든 반의 2과목 총점 : %d\n", total[2]);
}

void calc(int(*pscore)[100][3], int *ptotal)
{
	int i, j;

	for (i = 0; i < 10; i++)	// 10개반
	{
		for (j = 0; j < 100; j++)	// 100명
		{
			pscore[i][j][0] = 92;	//과목 점수
			pscore[i][j][1] = 90;
			pscore[i][j][2] = 95;
		}
	}
	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 100; j++)
		{
			ptotal[0] += pscore[i][j][0]; // ptotal이 가리키는 배열의 값에 대입
			ptotal[1] += pscore[i][j][1];
			ptotal[2] += pscore[i][j][2];
		}
	}

}

 


Result :