배열
배열은 공통 이름으로 저장하는 관련 항목의 그룹입니다. 다음은 배열을 함수에 인수로 전달하는 두 가지 방법입니다 -
- 전체 배열을 함수에 대한 인수로 보내기
- 개별 요소를 함수에 대한 인수로 보내기
전체 배열을 함수에 대한 인수로 보내기
-
전체 배열을 인수로 보내려면 함수 호출에서 배열 이름을 보내면 됩니다.
-
배열을 수신하려면 함수 헤더에 선언해야 합니다.
예시 1
#include<stdio.h>
main (){
void display (int a[5]);
int a[5], i;
clrscr();
printf ("enter 5 elements");
for (i=0; i<5; i++)
scanf("%d", &a[i]);
display (a); //calling array
getch( );
}
void display (int a[5]){
int i;
printf ("elements of the array are");
for (i=0; i<5; i++)
printf("%d ", a[i]);
} 출력
Enter 5 elements 10 20 30 40 50 Elements of the array are 10 20 30 40 50
예시 2
전체 배열을 함수에 대한 인수로 전달하는 방법에 대해 자세히 알아보기 위해 또 다른 예를 고려해 보겠습니다. -
#include<stdio.h>
main (){
void number(int a[5]);
int a[5], i;
printf ("enter 5 elements\n");
for (i=0; i<5; i++)
scanf("%d", &a[i]);
number(a); //calling array
getch( );
}
void number(int a[5]){
int i;
printf ("elements of the array are\n");
for (i=0; i<5; i++)
printf("%d\n" , a[i]);
} 출력
enter 5 elements 100 200 300 400 500 elements of the array are 100 200 300 400 500