อาร์เรย์
อาร์เรย์คือกลุ่มของรายการที่เกี่ยวข้องซึ่งจัดเก็บด้วยชื่อสามัญ ต่อไปนี้เป็นสองวิธีในการส่งอาร์เรย์เป็นอาร์กิวเมนต์ไปยังฟังก์ชัน -
- ส่งทั้งอาร์เรย์เป็นอาร์กิวเมนต์ไปยังฟังก์ชัน
- ส่งแต่ละองค์ประกอบเป็นอาร์กิวเมนต์ไปยังฟังก์ชัน
ส่งทั้งอาร์เรย์เป็นอาร์กิวเมนต์ไปยังฟังก์ชัน
-
ในการส่งทั้งอาร์เรย์เป็นอาร์กิวเมนต์ เพียงแค่ส่งชื่ออาร์เรย์ในการเรียกใช้ฟังก์ชัน
-
ในการรับอาร์เรย์ต้องประกาศไว้ในส่วนหัวของฟังก์ชัน
ตัวอย่างที่ 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