{"id":827,"date":"2015-02-25T00:45:33","date_gmt":"2015-02-24T22:45:33","guid":{"rendered":"http:\/\/www.blue.pri.ee\/ttu\/?page_id=827"},"modified":"2026-06-01T11:06:56","modified_gmt":"2026-06-01T09:06:56","slug":"alamstruktuur","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/programmeerimine-ii\/koodinaited\/alamstruktuur\/","title":{"rendered":"Alamstruktuuritel kasutamine"},"content":{"rendered":"<p data-select-like-a-boss=\"1\">Struktuuride pesastamisel on oluline, et kompilaator oleks struktuuri liikmena kasutatav andmet\u00fc\u00fcp juba &#8220;tuttav&#8221;. Praktikumides esitame pesastatud struktuuri definitsiooni esimesena, seej\u00e4rel defineerime struktuuri, mis seda sisaldab. Sama p\u00f5him\u00f5te on kasutusel ka j\u00e4rgnevas n\u00e4ites. Alternatiivina on v\u00f5imalik esitada alguses ainult struktuuri eeldeklaratsioon (<em>forward declaration<\/em>) v\u00f5i kirjutadagi struktuuri definitsioonid \u00fcksteise sisse. Nende lahenduste kohta v\u00f5id omal k\u00e4el juurde uurida.<\/p>\n<p data-select-like-a-boss=\"1\">Kokku on esitatud kolm n\u00e4idet, mis erinevad \u00fcksteisest muutuja algv\u00e4\u00e4rtustamise poolest<\/p>\n<ol>\n<li data-select-like-a-boss=\"1\">Esimeses n\u00e4ites kasutatakse omistamislauseid ja s\u00f5nede kopeerimist struktuuri v\u00e4\u00e4rtustamiseks<\/li>\n<li data-select-like-a-boss=\"1\">Teises n\u00e4ites on kasutatud struktuuri algv\u00e4\u00e4rtustamist<\/li>\n<li data-select-like-a-boss=\"1\">Kolmas n\u00e4ide kasutab\u00a0<em>compound literal<\/em>-i (toetab ka struktuuri v\u00e4\u00e4rtustamist tervikuna hiljem koodis).<\/li>\n<\/ol>\n<div class=\"su-tabs su-tabs-style-default su-tabs-mobile-stack\" data-active=\"1\" data-scroll-offset=\"0\" data-anchor-in-url=\"yes\"><div class=\"su-tabs-nav\"><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">N\u00e4ide 1: Omistamised<\/span><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">N\u00e4ide 2: Algv\u00e4\u00e4rtustamine<\/span><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">N\u00e4ide 3: <em>Compound literal<\/em><\/span><\/div><div class=\"su-tabs-panes\"><div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"N\u00e4ide 1: Omistamised\">\n<pre class=\"lang:c decode:true\">\/**\r\n * File:         car_return.c\r\n * Author:       Risto Heinsar\r\n * Created:      02.02.2015\r\n * Modified      31.01.2025\r\n *\r\n * Description:  This is an example code showing creation of a nested struct\r\n *\/\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\n#define REG_LEN 8\r\n#define FIELD_LEN 32\r\n\r\nstruct Vehicle\r\n{\r\n    char make[FIELD_LEN];\r\n    char model[FIELD_LEN];\r\n};\r\n\r\nstruct CarRegistryEntry\r\n{\r\n    char regNum[REG_LEN];\r\n    char regCity[FIELD_LEN];\r\n    char owner[FIELD_LEN];\r\n    int year;\r\n    struct Vehicle car;\r\n};\r\n\r\nvoid PrintCar(struct CarRegistryEntry motor);\r\nvoid PrintCarUsingPtr(struct CarRegistryEntry *car);\r\n\r\nint main(void)\r\n{\r\n    struct CarRegistryEntry indestructibleHilux;\r\n    strcpy(indestructibleHilux.regNum,\"E473CJN\");\r\n    strcpy(indestructibleHilux.regCity,\"London\");\r\n    strcpy(indestructibleHilux.owner,\"The Stig\");\r\n    indestructibleHilux.year = 1988;\r\n    strcpy(indestructibleHilux.car.make,\"Toyota\");\r\n    strcpy(indestructibleHilux.car.model,\"Hilux\");\r\n\r\n    PrintCar(indestructibleHilux);\r\n    PrintCarUsingPtr(&amp;indestructibleHilux);\r\n\r\n    return 0;\r\n}\r\n\r\n\/**\r\n * Description:    Outputs the details of an automobile using . notation\r\n *\r\n * Parameters:     motor, vehicle structure with the details of a car\r\n *\r\n * Return:         none\r\n *\/\r\nvoid PrintCar(struct CarRegistryEntry motor)\r\n{\r\n    printf(\"%s %s, reg number %s, belongs to %s and is registered in %s\\n\",\r\n                                        motor.car.make, motor.car.model, \r\n                                        motor.regNum, motor.owner, \r\n                                        motor.regCity);\r\n}\r\n\r\n\/**\r\n * Description:    Outputs the details of an automobile using -&gt; notation\r\n *\r\n * Parameters:     motor, vehicle structure with the details of a car\r\n *\r\n * Return:         none\r\n *\/\r\nvoid PrintCarUsingPtr(struct CarRegistryEntry *motor)\r\n{\r\n    printf(\"%s %s, reg number %s, belongs to %s and is registered in %s\\n\",\r\n                                        motor-&gt;car.make, motor-&gt;car.model,\r\n                                        motor-&gt;regNum, motor-&gt;owner,\r\n                                        motor-&gt;regCity);\r\n}\r\n<\/pre>\n<\/div>\n<div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"N\u00e4ide 2: Algv\u00e4\u00e4rtustamine\">\n<pre class=\"toolbar:2 lang:c decode:true\">\/**\r\n * File:         car_return_init_struct.c\r\n * Author:       Risto Heinsar\r\n * Created:      29.05.2026\r\n * Modified      29.05.2026\r\n *\r\n * Description:  This is an example code showing creation of a nested struct.\r\n * \r\n *               This example initializes all members explicitly\r\n *\/\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\n#define REG_LEN 8\r\n#define FIELD_LEN 32\r\n\r\nstruct Vehicle\r\n{\r\n    char make[FIELD_LEN];\r\n    char model[FIELD_LEN];\r\n};\r\n\r\nstruct CarRegistryEntry\r\n{\r\n    char regNum[REG_LEN];\r\n    char regCity[FIELD_LEN];\r\n    char owner[FIELD_LEN];\r\n    int year;\r\n    struct Vehicle car;\r\n};\r\n\r\nvoid PrintCar(struct CarRegistryEntry motor);\r\nvoid PrintCarUsingPtr(struct CarRegistryEntry *car);\r\n\r\nint main(void)\r\n{\r\n    struct CarRegistryEntry indestructibleHilux = \r\n                                    {.regNum = \"E473CJN\",\r\n                                     .regCity = \"London\",\r\n                                     .owner = \"The Stig\", \r\n                                     .year = 1988,\r\n                                     .car.make = \"Toyota\",\r\n                                     .car.model =\"Hilux\"};\r\n\r\n    PrintCar(indestructibleHilux);\r\n    PrintCarUsingPtr(&amp;indestructibleHilux);\r\n\r\n    return 0;\r\n}\r\n\r\n\/**\r\n * Description:    Outputs the details of an automobile using . notation\r\n *\r\n * Parameters:     motor, vehicle structure with the details of a car\r\n *\r\n * Return:         none\r\n *\/\r\nvoid PrintCar(struct CarRegistryEntry motor)\r\n{\r\n    printf(\"%s %s, reg number %s, belongs to %s and is registered in %s\\n\",\r\n                                        motor.car.make, motor.car.model, \r\n                                        motor.regNum, motor.owner, \r\n                                        motor.regCity);\r\n}\r\n\r\n\/**\r\n * Description:    Outputs the details of an automobile using -&gt; notation\r\n *\r\n * Parameters:     motor, vehicle structure with the details of a car\r\n *\r\n * Return:         none\r\n *\/\r\nvoid PrintCarUsingPtr(struct CarRegistryEntry *motor)\r\n{\r\n    printf(\"%s %s, reg number %s, belongs to %s and is registered in %s\\n\",\r\n                                        motor-&gt;car.make, motor-&gt;car.model,\r\n                                        motor-&gt;regNum, motor-&gt;owner,\r\n                                        motor-&gt;regCity);\r\n}\r\n<\/pre>\n<\/div>\n<div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"N\u00e4ide 3: &lt;em&gt;Compound literal&lt;\/em&gt;\">\n<pre class=\"toolbar:2 lang:c decode:true \">\/**\r\n * File:         car_return_compound_literal.c\r\n * Author:       Risto Heinsar\r\n * Created:      29.05.2026\r\n * Modified      29.05.2026\r\n *\r\n * Description:  This is an example code showing creation of a nested struct.\r\n * \r\n *               This example initializes all members explicitly\r\n *\/\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\n#define REG_LEN 8\r\n#define FIELD_LEN 32\r\n\r\nstruct Vehicle\r\n{\r\n    char make[FIELD_LEN];\r\n    char model[FIELD_LEN];\r\n};\r\n\r\nstruct CarRegistryEntry\r\n{\r\n    char regNum[REG_LEN];\r\n    char regCity[FIELD_LEN];\r\n    char owner[FIELD_LEN];\r\n    int year;\r\n    struct Vehicle car;\r\n};\r\n\r\nvoid PrintCar(struct CarRegistryEntry motor);\r\nvoid PrintCarUsingPtr(struct CarRegistryEntry *car);\r\n\r\nint main(void)\r\n{\r\n    struct CarRegistryEntry indestructibleHilux;\r\n    \r\n    indestructibleHilux = (struct CarRegistryEntry) {.regNum = \"E473CJN\",\r\n                                                     .regCity = \"London\",\r\n                                                     .owner = \"The Stig\", \r\n                                                     .year = 1988,\r\n                                                     .car.make = \"Toyota\",\r\n                                                     .car.model =\"Hilux\"};\r\n                                    \r\n\r\n    PrintCar(indestructibleHilux);\r\n    PrintCarUsingPtr(&amp;indestructibleHilux);\r\n\r\n    return 0;\r\n}\r\n\r\n\/**\r\n * Description:    Outputs the details of an automobile using . notation\r\n *\r\n * Parameters:     motor, vehicle structure with the details of a car\r\n *\r\n * Return:         none\r\n *\/\r\nvoid PrintCar(struct CarRegistryEntry motor)\r\n{\r\n    printf(\"%s %s, reg number %s, belongs to %s and is registered in %s\\n\",\r\n                                        motor.car.make, motor.car.model, \r\n                                        motor.regNum, motor.owner, \r\n                                        motor.regCity);\r\n}\r\n\r\n\/**\r\n * Description:    Outputs the details of an automobile using -&gt; notation\r\n *\r\n * Parameters:     motor, vehicle structure with the details of a car\r\n *\r\n * Return:         none\r\n *\/\r\nvoid PrintCarUsingPtr(struct CarRegistryEntry *motor)\r\n{\r\n    printf(\"%s %s, reg number %s, belongs to %s and is registered in %s\\n\",\r\n                                        motor-&gt;car.make, motor-&gt;car.model,\r\n                                        motor-&gt;regNum, motor-&gt;owner,\r\n                                        motor-&gt;regCity);\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<\/div><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Struktuuride pesastamisel on oluline, et kompilaator oleks struktuuri liikmena kasutatav andmet\u00fc\u00fcp juba &#8220;tuttav&#8221;. Praktikumides esitame pesastatud struktuuri definitsiooni esimesena, seej\u00e4rel defineerime struktuuri, mis seda sisaldab. Sama p\u00f5him\u00f5te on kasutusel ka j\u00e4rgnevas n\u00e4ites. Alternatiivina on v\u00f5imalik esitada alguses ainult struktuuri eeldeklaratsioon (forward declaration) v\u00f5i kirjutadagi struktuuri definitsioonid \u00fcksteise sisse. Nende lahenduste kohta v\u00f5id omal k\u00e4el juurde &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/programmeerimine-ii\/koodinaited\/alamstruktuur\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Alamstruktuuritel kasutamine<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":684,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width.php","meta":{"footnotes":""},"class_list":["post-827","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/827","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=827"}],"version-history":[{"count":7,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/827\/revisions"}],"predecessor-version":[{"id":11437,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/827\/revisions\/11437"}],"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=827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}