มีสามวิธีที่สามารถถ่ายโอนค่าของโครงสร้างจากฟังก์ชันหนึ่งไปยังอีกฟังก์ชันหนึ่งได้ ดังต่อไปนี้ −
-
ส่งผ่านสมาชิกแต่ละคนเป็นอาร์กิวเมนต์ในการทำงาน
-
ส่งผ่านโครงสร้างทั้งหมดเป็นอาร์กิวเมนต์ไปยังฟังก์ชัน
-
ส่งที่อยู่ของโครงสร้างเป็นอาร์กิวเมนต์ไปยังฟังก์ชัน
ตอนนี้เรามาดูวิธีการส่งผ่านโครงสร้างทั้งหมดเป็นอาร์กิวเมนต์ของฟังก์ชัน
-
ชื่อของตัวแปรโครงสร้างถูกกำหนดเป็นอาร์กิวเมนต์ในการเรียกใช้ฟังก์ชัน
-
มันถูกรวบรวมในตัวแปรโครงสร้างอื่นในส่วนหัวของฟังก์ชัน
-
ข้อเสียคือสำเนาของโครงสร้างทั้งหมดถูกสร้างขึ้นอีกครั้งโดยสิ้นเปลืองหน่วยความจำ
ตัวอย่าง
โปรแกรมต่อไปนี้แสดงวิธีการส่งผ่านโครงสร้างทั้งหมดเป็นอาร์กิวเมนต์ของฟังก์ชัน
#include<stdio.h>
struct date{
int day;
char month[10];
int year;
};
int main(){
struct date d;
printf("enter the day,month and year:");
scanf("%d%s%d",&d.day,d.month,&d.year);
display(d);//passing entire structure as an argument to function
return 0;
}
void display(struct date d){
printf("day=%d\n",d.day);
printf("month=%s\n",d.month);
printf("year=%d\n",d.year);
} ผลลัพธ์
เมื่อโปรแกรมข้างต้นทำงาน มันจะให้ผลลัพธ์ดังต่อไปนี้ −
enter the day, month and year:18 JAN 2021 day=18 month=JAN year=2021
ตัวอย่างที่ 2
ลองพิจารณาอีกตัวอย่างหนึ่ง ซึ่งจะมีการอธิบายโปรแกรม C เพื่อแสดงการผ่านของโครงสร้างทั้งหมดเป็นอาร์กิวเมนต์ของฟังก์ชัน
#include<stdio.h>
//Declaring structure//
struct add{
int var1;
int var2;
}a;
//Declaring and returning Function//
void show(struct add a){
//Declaring sum variable//
int sum;
//Arithmetic Operation//
sum=a.var1+a.var2;
//Printing O/p//
printf("Added value is %d",sum);
}
void main(){
//Declaring structure//
struct add a;
//Reading User I/p//
printf("Enter variable 1 = ");
scanf("%d",&a.var1);
printf("Enter variable 2 = ");
scanf("%d",&a.var2);
//Calling function//
show(a);
} ผลลัพธ์
เมื่อโปรแกรมข้างต้นทำงาน มันจะให้ผลลัพธ์ดังต่อไปนี้ −
Enter variable 1 = 20 Enter variable 2 = 50 Added value is 70
ตัวอย่างที่ 3
มีโปรแกรม C อีกโปรแกรมหนึ่งที่แสดงการผ่านของโครงสร้างทั้งหมดเป็นอาร์กิวเมนต์ของฟังก์ชัน ซึ่งอธิบายโครงสร้างการประกาศ การประกาศ และการส่งคืนฟังก์ชัน ฯลฯ
#include<stdio.h>
//Declaring structure//
struct student{
int s1,s2,s3;
}s[5];
//Declaring and returning Function//
void addition(struct student s[]){
//Declaring sum variable and For loop variable//
int i,sum;
//Arithmetic Operation//
for(i=1;i<4;i++){
sum=s[i].s1+s[i].s2+s[i].s3;
printf("Student %d scored total of %d\n",i,sum);
}
}
void main(){
//Declaring variable for For loop//
int i;
//Reading User I/p through For loop//
for(i=1;i<4;i++){
printf("Enter marks for student %d in subject 1 = ",i);
scanf("%d",&s[i].s1);
printf("Enter marks for student %d in subject 2 = ",i);
scanf("%d",&s[i].s2);
printf("Enter marks for student %d in subject 3 = ",i);
scanf("%d",&s[i].s3);
}
//Calling function//
addition(s);
} ผลลัพธ์
เมื่อโปรแกรมข้างต้นทำงาน มันจะให้ผลลัพธ์ดังต่อไปนี้ −
Enter marks for student 1 in subject 1 = 25 Enter marks for student 1 in subject 2 = 89 Enter marks for student 1 in subject 3 = 45 Enter marks for student 2 in subject 1 = 12 Enter marks for student 2 in subject 2 = 45 Enter marks for student 2 in subject 3 = 89 Enter marks for student 3 in subject 1 = 12 Enter marks for student 3 in subject 2 = 78 Enter marks for student 3 in subject 3 = 12 Student 1 scored total of 159 Student 2 scored total of 146 Student 3 scored total of 102