Struktuuri tagastusega funktsioon
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 |
/** * File: car_return.c * Author: Risto Heinsar * Created: 02.02.2015 * Modified 16.02.2017 * * Description: This is a sample code for the subject IAG0582 * Reads the car details into a structure and returns * the structure from a function */ #include <stdio.h> #include <stdlib.h> #define REG_LEN 8 #define FIELD_LEN 32 typedef struct car { char regNum[REG_LEN]; char regCity[FIELD_LEN]; char manufacturer[FIELD_LEN]; char model[FIELD_LEN]; int year; } car; car EnterCarData(void); void PrintCarData(car autoMobile); int main(void) { car machine; machine = EnterCarData(); PrintCarData(machine); return 0; } /** * Description: Reads the details of a car into a structure and returns it. * * Parameters: none * * Return: car structure, details of an automobile. */ car EnterCarData(void) { car vehicle; char temp[FIELD_LEN]; printf("Enter the registration number of the car (max %d symbols)\n", REG_LEN - 1); gets(vehicle.regNum); printf("Enter the registration city of the car (max %d symbols)\n", FIELD_LEN - 1); gets(vehicle.regCity); printf("Enter the manufacturer of the car (max %d symbols)\n", FIELD_LEN - 1); gets(vehicle.manufacturer); printf("Enter the model number of the car (max %d symbols)\n", FIELD_LEN - 1); gets(vehicle.model); printf("Enter the year of manufacturing (max %d symbols)\n", FIELD_LEN - 1); vehicle.year = atoi(gets(temp)); return vehicle; } /** * Description: Outputs the details of an automobile * * Parameters: automobile, car structure with the details of a car * * Return: none */ void PrintCarData(car autoMobile) { printf("%s %s %s %s %d\n", autoMobile.regNum, autoMobile.regCity, autoMobile.manufacturer, autoMobile.model, autoMobile.year); } |
Struktuur struktuuris
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 |
/** * File: car_return.c * Author: Risto Heinsar * Created: 02.02.2015 * Modified 16.02.2017 * * Description: This is a sample code for the subject IAG0582 * Shows the use of a substructure */ #include <stdio.h> #include <string.h> #define REG_LEN 8 #define FIELD_LEN 32 typedef struct vehicle { char make[FIELD_LEN]; char model[FIELD_LEN]; } vehicle; typedef struct carRegistryEntry { char regNum[REG_LEN]; char regCity[FIELD_LEN]; char owner[FIELD_LEN]; int year; vehicle car; } carRegistryEntry; void PrintCar(carRegistryEntry motor); void PrintCarUsingPtr(carRegistryEntry *car); int main(void) { 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(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(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); } |