{"id":7926,"date":"2023-01-04T18:28:29","date_gmt":"2023-01-04T16:28:29","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?page_id=7926"},"modified":"2026-01-21T21:49:47","modified_gmt":"2026-01-21T19:49:47","slug":"struktuuri-tagastus","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/programmeerimine-ii\/koodinaited\/struktuuri-tagastus\/","title":{"rendered":"Struktuuri tagastus funktsioonist"},"content":{"rendered":"<p>J\u00e4rgnev kood on n\u00e4idis kuidas tagastada struktuuri eksemplari. N\u00e4ite juures tasub m\u00f5elda j\u00e4rgnevale<\/p>\n<ul>\n<li>Struktuur luuakse funktsioonis kus seda hakatakse v\u00e4\u00e4rtustama ning seej\u00e4rel tagastatakse.<\/li>\n<li>Struktuuri t\u00e4itmine eraldi funktsioonis annab meile v\u00f5imaluse kasutada k\u00f5ikjal veakontrolle (kui konkreetset struktuurielementi luuakse, olenemata kohast)<\/li>\n<li>Lahendus sobib kenasti ka struktuurimassiivi iga liikme lugemiseks.<\/li>\n<li>Lahendus on sobilik ka failidest lugemisel<\/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>J\u00e4rgnev kood on n\u00e4idis kuidas tagastada struktuuri eksemplari. N\u00e4ite juures tasub m\u00f5elda j\u00e4rgnevale Struktuur luuakse funktsioonis kus seda hakatakse v\u00e4\u00e4rtustama ning seej\u00e4rel tagastatakse. Struktuuri t\u00e4itmine eraldi funktsioonis annab meile v\u00f5imaluse kasutada k\u00f5ikjal veakontrolle (kui konkreetset struktuurielementi luuakse, olenemata kohast) Lahendus sobib kenasti ka struktuurimassiivi iga liikme lugemiseks. Lahendus on sobilik ka failidest lugemisel \/** * &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/programmeerimine-ii\/koodinaited\/struktuuri-tagastus\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Struktuuri tagastus funktsioonist<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":684,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width-wide.php","meta":{"footnotes":""},"class_list":["post-7926","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/7926","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=7926"}],"version-history":[{"count":4,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/7926\/revisions"}],"predecessor-version":[{"id":11060,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/7926\/revisions\/11060"}],"up":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/684"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=7926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}