{"id":7930,"date":"2023-01-04T18:31:10","date_gmt":"2023-01-04T16:31:10","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?page_id=7930"},"modified":"2026-01-21T21:49:53","modified_gmt":"2026-01-21T19:49:53","slug":"returning-a-struct","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/programming-ii\/code-samples\/returning-a-struct\/","title":{"rendered":"Returning a struct"},"content":{"rendered":"<p>This example shows you how to return a structure from a function.<\/p>\n<p>Think of the following when reading this:<\/p>\n<ul>\n<li>The struct is created locally and return.<\/li>\n<li>By creating the struct in a dedicated function, we can easily implement error checking that will always be present.<\/li>\n<li>This also works nicely if you need to read multiple structs into a struct array.<\/li>\n<li>This also works for when reading from files.<\/li>\n<\/ul>\n<pre class=\"toolbar:1 lang:default decode:true \">\/**\r\n * File:         car_return.c\r\n * Author:       Risto Heinsar\r\n * Created:      02.02.2015\r\n * Modified      21.01.2026\r\n *\r\n * Description:  Example how to return a copy of a struct from a function\r\n *\/\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\n#define REG_LEN 10\r\n#define FIELD_LEN 128\r\n\r\nstruct Car\r\n{\r\n    char regNum[REG_LEN];\r\n    char regCity[FIELD_LEN];\r\n    char manufacturer[FIELD_LEN];\r\n    char model[FIELD_LEN];\r\n    int year;\r\n};\r\n\r\nstruct Car EnterCarData(void);\r\nvoid PrintCarData(struct Car autoMobile);\r\nvoid GetString(char str[], int len);\r\nint GetPositiveInt(void);\r\n\r\nint main(void)\r\n{\r\n    \/* Store the returned struct *\/\r\n    struct Car machine = EnterCarData();\r\n    \r\n    PrintCarData(machine);\r\n    return EXIT_SUCCESS;\r\n}\r\n\r\n\r\n\/**\r\n * Description:    Reads the details of a car into a structure and returns it.\r\n *\r\n * Parameters:     none\r\n *\r\n * Return:         car structure, details of an automobile.\r\n *\/\r\nstruct Car EnterCarData(void)\r\n{\r\n    \/* Declare a temporary struct *\/\r\n    struct Car vehicle;\r\n    \r\n    \/* Populate the members of the struct *\/\r\n    printf(\"Enter the registration number of the car\\n\");\r\n    GetString(vehicle.regNum, REG_LEN);\r\n    \r\n    printf(\"Enter the registration city of the car\\n\");\r\n    GetString(vehicle.regCity, FIELD_LEN);\r\n    \r\n    printf(\"Enter the manufacturer of the car\\n\");\r\n    GetString(vehicle.manufacturer, FIELD_LEN);\r\n    \r\n    printf(\"Enter the model number of the car\\n\");\r\n    GetString(vehicle.model, FIELD_LEN);\r\n    \r\n    printf(\"Enter the year of manufacturing\\n\");\r\n    vehicle.year = GetPositiveInt();\r\n    \r\n    return vehicle;\r\n}\r\n\r\n\/**\r\n * Description:    Outputs the details of an automobile\r\n *\r\n * Parameters:     automobile, car structure with the details of a car\r\n *\r\n * Return:         none\r\n *\/\r\nvoid PrintCarData(struct Car autoMobile) \r\n{\r\n    printf(\"%s %s %s %s %d\\n\", autoMobile.regNum,       \r\n                               autoMobile.regCity,\r\n                               autoMobile.manufacturer, \r\n                               autoMobile.model, \r\n                               autoMobile.year);\r\n}\r\n\r\n\/**\r\n * Description:    Reads a positive int from the keyboard, reprompts if \r\n *                 input is not positive\r\n *\r\n * Parameters:     none\r\n *\r\n * Return:         Positive integer from stdin\r\n *\/\r\nint GetPositiveInt(void)\r\n{\r\n    char temp[FIELD_LEN];\r\n\r\n    while (1)\r\n    {\r\n        \/* Get value as a string  and convert to int *\/\r\n        GetString(temp, FIELD_LEN);\r\n        int ans = atoi(temp);\r\n        \r\n        \/* Return if result matches criteria*\/\r\n        if (ans &gt; 0)\r\n            return ans;\r\n            \r\n        \/* Reprompt on invalid input *\/\r\n        printf(\"Error! Input must be a positive number!\\n\");\r\n    }\r\n}\r\n\r\n\/**\r\n * Description:    Reads a string from stdin and fixes the newline character\r\n *\r\n * Parameters:     str - read string to be stored here\r\n *                 len - maximum length for str\r\n *\r\n * Return:         none\r\n *\/\r\nvoid GetString(char *str, int len)\r\n{\r\n    \/* Grab user input *\/\r\n    fgets(str, len, stdin);\r\n    \r\n    \/* Identify the newline (if present) *\/\r\n    char *pStr = str;\r\n    while (*pStr != '\\n' &amp;&amp; *pStr != '\\0')\r\n        pStr++;\r\n    \r\n    \/* Get rid of the trailing newline *\/\r\n    *pStr = '\\0';\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/programming-ii\/code-samples\/returning-a-struct\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Returning a struct<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2361,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width-wide.php","meta":{"footnotes":""},"class_list":["post-7930","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/7930","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/comments?post=7930"}],"version-history":[{"count":3,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/7930\/revisions"}],"predecessor-version":[{"id":11061,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/7930\/revisions\/11061"}],"up":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/2361"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=7930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}