Štruktúra a funkcie C.

V tomto tutoriáli sa naučíte odovzdávať štruktúrne premenné ako argumenty funkcii. Naučiť sa vracať štruktúrne z funkcie pomocou príkladov.

Podobne ako v prípade premenných zabudovaných typov, aj do funkcie môžete odovzdať štruktúrne premenné.

Odovzdávanie štruktúr do funkcií

Odporúčame vám, aby ste si tieto návody osvojili skôr, ako sa naučíte, ako prenášať štruktúry do funkcií.

  • C štruktúry
  • Funkcie C.
  • Užívateľom definovaná funkcia

Tu je príklad, ako môžete funkcii odovzdať štruktúry

 #include struct student ( char name(50); int age; ); // function prototype void display(struct student s); int main() ( struct student s1; printf("Enter name: "); // read string input from the user until is entered // is discarded scanf("%(^)%*c", s1.name); printf("Enter age: "); scanf("%d", &s1.age); display(s1); // passing struct as an argument return 0; ) void display(struct student s) ( printf("Displaying information"); printf("Name: %s", s.name); printf("Age: %d", s.age); )

Výkon

 Zadajte meno: Bond Zadajte vek: 13 Zobrazené informácie Meno: Bond Bond: 13 

Tu sa vytvorí štruktúrna premenná typu s1 struct student. Premenná sa do display()funkcie odovzdá pomocou display(s1);príkazu.

Vrátiť štruktúru z funkcie

Tu je príklad, ako môžete vrátiť štruktúru z funkcie:

 #include struct student ( char name(50); int age; ); // function prototype struct student getInformation(); int main() ( struct student s; s = getInformation(); printf("Displaying information"); printf("Name: %s", s.name); printf("Roll: %d", s.age); return 0; ) struct student getInformation() ( struct student s1; printf("Enter name: "); scanf ("%(^)%*c", s1.name); printf("Enter age: "); scanf("%d", &s1.age); return s1; ) 

Tu sa getInformation()funkcia volá using s = getInformation();statement. Funkcia vráti štruktúru typu struct student. Vrátená štruktúra sa zobrazuje z main()funkcie.

Všimnite si, že návratový typ getInformation()je tiež struct student.

Absolvovanie štruktúry odkazom

Môžete tiež odovzdať štruktúry odkazom (podobným spôsobom, ako keby ste prechádzali premenné zabudovaného typu odkazom). Než budete pokračovať, odporúčame vám prečítať si referenčný návod.

Počas prechádzania odkazom sa funkcii odovzdávajú adresy pamäte štruktúrnych premenných.

 #include typedef struct Complex ( float real; float imag; ) complex; void addNumbers(complex c1, complex c2, complex *result); int main() ( complex c1, c2, result; printf("For first number,"); printf("Enter real part: "); scanf("%f", &c1.real); printf("Enter imaginary part: "); scanf("%f", &c1.imag); printf("For second number, "); printf("Enter real part: "); scanf("%f", &c2.real); printf("Enter imaginary part: "); scanf("%f", &c2.imag); addNumbers(c1, c2, &result); printf("result.real = %.1f", result.real); printf("result.imag = %.1f", result.imag); return 0; ) void addNumbers(complex c1, complex c2, complex *result) ( result->real = c1.real + c2.real; result->imag = c1.imag + c2.imag; ) 

Výkon

 Pre prvé číslo zadajte skutočnú časť: 1,1 Zadajte imaginárnu časť: -2,4. Pre druhé číslo zadajte skutočnú časť: 3,4 Zadajte imaginárnu časť: -3,2 výsledok. Skutočný = 4,5 výsledok. Obrázok = -5,6 

Vo vyššie uvedenom programe sú addNumbers()funkcii odovzdané tri štruktúrne premenné c1, c2 a adresa výsledku . Tu je výsledok odovzdaný odkazom.

Keď addNumbers()sa zmení výsledná premenná vo vnútri, zodpovedajúcim spôsobom sa zmení aj výsledná premenná vo vnútri main()funkcie.

Zaujímavé články...