Struktuuride pesastamisel on oluline, et kompilaator oleks struktuuri liikmena kasutatav andmetüüp juba “tuttav”. Praktikumides esitame pesastatud struktuuri definitsiooni esimesena, seejärel defineerime struktuuri, mis seda sisaldab. Sama põhimõte on kasutusel ka järgnevas näites. Alternatiivina on võimalik esitada alguses ainult struktuuri eeldeklaratsioon (forward declaration) või kirjutadagi struktuuri definitsioonid üksteise sisse. Nende lahenduste kohta võid omal käel juurde uurida.
Kokku on esitatud kolm näidet, mis erinevad üksteisest muutuja algväärtustamise poolest
- Esimeses näites kasutatakse omistamislauseid ja sõnede kopeerimist struktuuri väärtustamiseks
- Teises näites on kasutatud struktuuri algväärtustamist
- Kolmas näide kasutab compound literal-i (toetab ka struktuuri väärtustamist tervikuna hiljem koodis).
Näide 1: OmistamisedNäide 2: AlgväärtustamineNäide 3: Compound literal
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
/** * File: car_return.c * Author: Risto Heinsar * Created: 02.02.2015 * Modified 31.01.2025 * * Description: This is an example code showing creation of a nested struct */ #include <stdio.h> #include <string.h> #define REG_LEN 8 #define FIELD_LEN 32 struct Vehicle { char make[FIELD_LEN]; char model[FIELD_LEN]; }; struct CarRegistryEntry { char regNum[REG_LEN]; char regCity[FIELD_LEN]; char owner[FIELD_LEN]; int year; struct Vehicle car; }; void PrintCar(struct CarRegistryEntry motor); void PrintCarUsingPtr(struct CarRegistryEntry *car); int main(void) { struct CarRegistryEntry indestructibleHilux; strcpy(indestructibleHilux.regNum,"E473CJN"); strcpy(indestructibleHilux.regCity,"London"); strcpy(indestructibleHilux.owner,"The Stig"); indestructibleHilux.year = 1988; strcpy(indestructibleHilux.car.make,"Toyota"); strcpy(indestructibleHilux.car.model,"Hilux"); PrintCar(indestructibleHilux); PrintCarUsingPtr(&indestructibleHilux); return 0; } /** * Description: Outputs the details of an automobile using . notation * * Parameters: motor, vehicle structure with the details of a car * * Return: none */ void PrintCar(struct CarRegistryEntry motor) { printf("%s %s, reg number %s, belongs to %s and is registered in %s\n", motor.car.make, motor.car.model, motor.regNum, motor.owner, motor.regCity); } /** * Description: Outputs the details of an automobile using -> notation * * Parameters: motor, vehicle structure with the details of a car * * Return: none */ void PrintCarUsingPtr(struct CarRegistryEntry *motor) { printf("%s %s, reg number %s, belongs to %s and is registered in %s\n", motor->car.make, motor->car.model, motor->regNum, motor->owner, motor->regCity); } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
/** * File: car_return_init_struct.c * Author: Risto Heinsar * Created: 29.05.2026 * Modified 29.05.2026 * * Description: This is an example code showing creation of a nested struct. * * This example initializes all members explicitly */ #include <stdio.h> #include <string.h> #define REG_LEN 8 #define FIELD_LEN 32 struct Vehicle { char make[FIELD_LEN]; char model[FIELD_LEN]; }; struct CarRegistryEntry { char regNum[REG_LEN]; char regCity[FIELD_LEN]; char owner[FIELD_LEN]; int year; struct Vehicle car; }; void PrintCar(struct CarRegistryEntry motor); void PrintCarUsingPtr(struct CarRegistryEntry *car); int main(void) { struct CarRegistryEntry indestructibleHilux = {.regNum = "E473CJN", .regCity = "London", .owner = "The Stig", .year = 1988, .car.make = "Toyota", .car.model ="Hilux"}; PrintCar(indestructibleHilux); PrintCarUsingPtr(&indestructibleHilux); return 0; } /** * Description: Outputs the details of an automobile using . notation * * Parameters: motor, vehicle structure with the details of a car * * Return: none */ void PrintCar(struct CarRegistryEntry motor) { printf("%s %s, reg number %s, belongs to %s and is registered in %s\n", motor.car.make, motor.car.model, motor.regNum, motor.owner, motor.regCity); } /** * Description: Outputs the details of an automobile using -> notation * * Parameters: motor, vehicle structure with the details of a car * * Return: none */ void PrintCarUsingPtr(struct CarRegistryEntry *motor) { printf("%s %s, reg number %s, belongs to %s and is registered in %s\n", motor->car.make, motor->car.model, motor->regNum, motor->owner, motor->regCity); } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
/** * File: car_return_compound_literal.c * Author: Risto Heinsar * Created: 29.05.2026 * Modified 29.05.2026 * * Description: This is an example code showing creation of a nested struct. * * This example initializes all members explicitly */ #include <stdio.h> #include <string.h> #define REG_LEN 8 #define FIELD_LEN 32 struct Vehicle { char make[FIELD_LEN]; char model[FIELD_LEN]; }; struct CarRegistryEntry { char regNum[REG_LEN]; char regCity[FIELD_LEN]; char owner[FIELD_LEN]; int year; struct Vehicle car; }; void PrintCar(struct CarRegistryEntry motor); void PrintCarUsingPtr(struct CarRegistryEntry *car); int main(void) { struct CarRegistryEntry indestructibleHilux; indestructibleHilux = (struct CarRegistryEntry) {.regNum = "E473CJN", .regCity = "London", .owner = "The Stig", .year = 1988, .car.make = "Toyota", .car.model ="Hilux"}; PrintCar(indestructibleHilux); PrintCarUsingPtr(&indestructibleHilux); return 0; } /** * Description: Outputs the details of an automobile using . notation * * Parameters: motor, vehicle structure with the details of a car * * Return: none */ void PrintCar(struct CarRegistryEntry motor) { printf("%s %s, reg number %s, belongs to %s and is registered in %s\n", motor.car.make, motor.car.model, motor.regNum, motor.owner, motor.regCity); } /** * Description: Outputs the details of an automobile using -> notation * * Parameters: motor, vehicle structure with the details of a car * * Return: none */ void PrintCarUsingPtr(struct CarRegistryEntry *motor) { printf("%s %s, reg number %s, belongs to %s and is registered in %s\n", motor->car.make, motor->car.model, motor->regNum, motor->owner, motor->regCity); } |