Computer >> 컴퓨터 >  >> 프로그램 작성 >> C 프로그래밍

배열의 개별 요소를 C 언어의 함수에 대한 인수로 전달하는 방법은 무엇입니까?

<시간/>

개별 요소가 인수로 전달되는 경우 배열 요소와 해당 첨자를 함수 호출에 제공해야 합니다.

요소를 받기 위해 함수 정의에서 간단한 변수를 사용합니다.

예시 1

#include<stdio.h>
main (){
   void display (int, int);
   int a[5], i;
   clrscr();
   printf (“enter 5 elements”);
   for (i=0; i<5; i++)
      scanf("%d", &a[i]);
   display (a [0], a[4]); //calling function with individual arguments
   getch( );
}
void display (int a, int b){ //called function with individual arguments
   print f ("first element = %d",a);
   printf ("last element = %d",b);
}

출력

Enter 5 elements
   10    20    30    40    50
First element = 10
Last element = 50

예시 2

개별 요소를 함수에 대한 인수로 전달하는 또 다른 예를 고려하십시오.

#include<stdio.h>
main (){
   void arguments(int,int);
   int a[10], i;
   printf ("enter 6 elements:\n");
   for (i=0; i<6; i++)
      scanf("%d", &a[i]);
   arguments(a[0],a[5]); //calling function with individual arguments
   getch( );
}
void arguments(int a, int b){ //called function with individual arguments
   printf ("first element = %d\n",a);
   printf ("last element = %d\n",b);
}

출력

enter 6 elements:
1
2
3
4
5
6
first element = 1
last element = 6