มีสามวิธีที่สามารถถ่ายโอนค่าของโครงสร้างจากฟังก์ชันหนึ่งไปยังอีกฟังก์ชันหนึ่งได้ ดังต่อไปนี้ −
-
ส่งผ่านสมาชิกแต่ละคนเป็นอาร์กิวเมนต์ในการทำงาน
-
ส่งผ่านโครงสร้างทั้งหมดเป็นอาร์กิวเมนต์ไปยังฟังก์ชัน
-
ส่งที่อยู่ของโครงสร้างเป็นอาร์กิวเมนต์ไปยังฟังก์ชัน
ตอนนี้ ให้เราเข้าใจวิธีส่งที่อยู่ของโครงสร้างเป็นอาร์กิวเมนต์ไปยังฟังก์ชัน
-
ที่อยู่ของโครงสร้างจะถูกส่งผ่านเป็นอาร์กิวเมนต์ของฟังก์ชัน
-
มันถูกรวบรวมไว้ในตัวชี้ไปยังโครงสร้างในส่วนหัวของฟังก์ชัน
ข้อดี
ข้อดีของการส่งที่อยู่ของโครงสร้างเป็นอาร์กิวเมนต์ไปยังฟังก์ชันมีดังนี้ -
-
ไม่มีการสูญเสียหน่วยความจำเนื่องจากไม่จำเป็นต้องสร้างสำเนาอีกครั้ง
-
ไม่จำเป็นต้องคืนค่า เนื่องจากฟังก์ชันสามารถเข้าถึงโครงสร้างทั้งหมดทางอ้อมแล้วทำงานกับมันได้
ตัวอย่าง
โปรแกรมต่อไปนี้แสดงวิธีส่งที่อยู่ของโครงสร้างเป็นอาร์กิวเมนต์ไปยังฟังก์ชัน -
#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);
return 0;
}
void display(struct date *p){
printf("day=%d\n",p->day);
printf("month=%s\n",p->month);
printf("year=%d\n",p->year);
} ผลลัพธ์
เมื่อโปรแกรมข้างต้นทำงาน มันจะให้ผลลัพธ์ดังต่อไปนี้ −
enter the day, month and year:20 MAR 2021 day=20 month=MAR year=2021
ตัวอย่าง
รับด้านล่างเป็นโปรแกรม C เพื่อแสดงโครงสร้างและหน้าที่โดยเรียกฟังก์ชันทั้งหมดเป็นอาร์กิวเมนต์ ด้วยวิธีการเรียกฟังก์ชันนี้ จึงไม่เปลืองหน่วยความจำ เนื่องจากเราไม่จำเป็นต้องคัดลอกอีกครั้งและคืนค่ากลับ
#include<stdio.h>
//Declaring structure//
struct student{
char Name[100];
int Age;
float Level;
char Grade[50];
char temp;
}s[5];
//Declaring and returning Function//
void show(struct student *p){
//Declaring variable for For loop within the function//
int i;
//For loop for printing O/p//
for(i=1;i<3;i++){
printf("The Name of student %d is : %s\n",i,p->Name);
printf("The Age of student %d is : %d\n",i,p->Age);
printf("The Level of student %d is : %f\n",i,p->Level);
printf("The Grade of student %d is : %s\n",i,p->Grade);
p++;
}
}
void main(){
//Declaring variable for for loop//
int i;
//Declaring structure with pointer//
struct student *p;
//Reading User I/p//
for(i=0;i<2;i++){
printf("Enter the Name of student %d : ",i+1);
gets(s[i].Name);
printf("Enter the Age of student %d : ",i+1);
scanf("%d",&s[i].Age);
printf("Enter the Level of student %d :",i+1);
scanf("%f",&s[i].Level);
scanf("%c",&s[i].temp);//Clearing Buffer//
printf("Enter the Grade of student %d :",i+1);
gets(s[i].Grade);
}
//Assigning pointer to structure//
p=&s;
//Calling function//
show(&s);
} ผลลัพธ์
เมื่อโปรแกรมข้างต้นทำงาน มันจะให้ผลลัพธ์ดังต่อไปนี้ −
Enter the Name of student 1 : Lucky Enter the Age of student 1 : 27 Enter the Level of student 1 :2 Enter the Grade of student 1 :A Enter the Name of student 2 : Pinky Enter the Age of student 2 : 29 Enter the Level of student 2 :1 Enter the Grade of student 2 :B The Name of student 1 is : Lucky The Age of student 1 is : 27 The Level of student 1 is : 2.000000 The Grade of student 1 is : A The Name of student 2 is : Pinky The Age of student 2 is : 29 The Level of student 2 is : 1.000000 The Grade of student 2 is : B