V tomto kurzu se naučíte předávat strukturní proměnné jako argumenty funkci. Naučíte se vrátit strukturu z funkce pomocí příkladů.
Podobně jako u proměnných vestavěných typů můžete také předat proměnné struktury do funkce.
Předávání struktur funkcím
Doporučujeme vám, abyste se naučili tyto výukové programy, než se naučíte předávat struktury funkcím.
- C struktury
- Funkce C.
- Uživatelem definovaná funkce
Tady je způsob, jak můžete funkci předat struktury
#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ýstup
Zadejte jméno: Bond Zadejte věk: 13 Zobrazit informace Název: Bond Age: 13
Zde se vytvoří strukturní proměnná s1 typu struct student
. Proměnná je předána display()
funkci pomocí display(s1);
příkazu.
Vrátí strukturu z funkce
Tady je způsob, jak můžete vrátit strukturu z funkce:
#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; )
Zde se getInformation()
funkce nazývá using s = getInformation();
statement. Funkce vrací strukturu typu struct student
. Vrácená struktura se zobrazí z main()
funkce.
Všimněte si, že návratový typ getInformation()
je také struct student
.
Předávání struktury odkazem
Můžete také předat struktury odkazem (podobným způsobem, jako byste předávali proměnné vestavěného typu odkazem). Než budete pokračovat, doporučujeme vám přečíst si referenční příručku.
Během předávání odkazem jsou paměťové adresy strukturních proměnných předávány funkci.
#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ýstup
Pro první číslo zadejte skutečnou část: 1,1 Zadejte imaginární část: -2,4 Pro druhé číslo zadejte skutečnou část: 3,4 Zadejte imaginární část: -3,2 výsledek. Skutečný = 4,5 výsledek. Obrázek = -5,6
Ve výše uvedeném programu jsou addNumbers()
funkci předány tři strukturní proměnné c1, c2 a adresa výsledku . Zde je výsledek předán odkazem.
Když addNumbers()
se změní proměnná výsledku uvnitř, main()
změní se odpovídajícím způsobem také proměnná výsledku uvnitř funkce.