{"id":2552,"date":"2016-02-16T06:13:45","date_gmt":"2016-02-16T04:13:45","guid":{"rendered":"http:\/\/blue.pri.ee\/ttu\/?page_id=2552"},"modified":"2026-01-21T21:52:18","modified_gmt":"2026-01-21T19:52:18","slug":"substruct","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/programming-ii\/code-samples\/substruct\/","title":{"rendered":"Substruct example"},"content":{"rendered":"<p data-select-like-a-boss=\"1\">When nesting structures, the compiler needs to recognize the struct type you intend to use as a member. This can either be by giving a full declaration of the struct (show in in the example) or by providing a forward declaration only, assuming that a full declaration is provided at a different location.<\/p>\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<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When nesting structures, the compiler needs to recognize the struct type you intend to use as a member. This can either be by giving a full declaration of the struct (show in in the example) or by providing a forward declaration only, assuming that a full declaration is provided at a different location. \/** * &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/programming-ii\/code-samples\/substruct\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Substruct example<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2361,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width.php","meta":{"footnotes":""},"class_list":["post-2552","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/2552","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=2552"}],"version-history":[{"count":5,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/2552\/revisions"}],"predecessor-version":[{"id":11063,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/2552\/revisions\/11063"}],"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=2552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}