Järgnev kood on näidis kuidas tagastada struktuuri eksemplari. Näite juures tasub mõelda järgnevale
- Struktuur luuakse funktsioonis kus seda hakatakse väärtustama ning seejärel tagastatakse.
- Struktuuri täitmine eraldi funktsioonis annab meile võimaluse kasutada kõikjal veakontrolle (kui konkreetset struktuurielementi luuakse, olenemata kohast)
- Lahendus sobib kenasti ka struktuurimassiivi iga liikme lugemiseks.
- Lahendus on sobilik ka failidest lugemisel
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
/** * File: car_return.c * Author: Risto Heinsar * Created: 02.02.2015 * Modified 31.01.2025 * * Description: Example how to return a copy of a struct from a function */ #include <stdio.h> #include <stdlib.h> #define REG_LEN 10 #define FIELD_LEN 128 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); void GetString(char str[], int len); int GetPositiveInt(void); int main(void) { /* Store the returned struct */ Car machine = EnterCarData(); PrintCarData(machine); return EXIT_SUCCESS; } /** * 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) { /* Declare a temporary struct */ Car vehicle; /* Populate the members of the struct */ printf("Enter the registration number of the car\n"); GetString(vehicle.regNum, REG_LEN); printf("Enter the registration city of the car\n"); GetString(vehicle.regCity, FIELD_LEN); printf("Enter the manufacturer of the car\n"); GetString(vehicle.manufacturer, FIELD_LEN); printf("Enter the model number of the car\n"); GetString(vehicle.model, FIELD_LEN); printf("Enter the year of manufacturing\n"); vehicle.year = GetPositiveInt(); 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); } /** * Description: Reads a positive int from the keyboard, reprompts if * input is not positive * * Parameters: none * * Return: Positive integer from stdin */ int GetPositiveInt(void) { char temp[FIELD_LEN]; while (1) { /* Get value as a string and convert to int */ GetString(temp, FIELD_LEN); int ans = atoi(temp); /* Return if result matches criteria*/ if (ans > 0) return ans; /* Reprompt on invalid input */ printf("Error! Input must be a positive number!\n"); } } /** * Description: Reads a string from stdin and fixes the newline character * * Parameters: str - read string to be stored here * len - maximum length for str * * Return: none */ void GetString(char *str, int len) { /* Grab user input */ fgets(str, len, stdin); /* Identify the newline (if present) */ char *pStr = str; while (*pStr != '\n' && *pStr != '\0') pStr++; /* Get rid of the trailing newline */ *pStr = '\0'; } |