This example shows you how to return a structure from a function.
Think of the following when reading this:
- The struct is created locally and return.
- By creating the struct in a dedicated function, we can easily implement error checking that will always be present.
- This also works nicely if you need to read multiple structs into a struct array.
- This also works for when reading from files.
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 13.02.2024 * * 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'; } |