{"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-06-01T11:09:07","modified_gmt":"2026-06-01T09:09:07","slug":"substruct","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/programming-ii\/code-samples\/substruct\/","title":{"rendered":"Nested structure 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. In the labs, first the inner struct will be defined, followed by defining the outer struct that contains the previously defined struct. This principle is also followed in the examples below. Alternatively, a forward declaration can be provided or the structs can be written physically nested already in the definition. You can look up those examples on your own if interested.<\/p>\n<p data-select-like-a-boss=\"1\">There are are total of three examples provided<\/p>\n<ol>\n<li data-select-like-a-boss=\"1\">Using assignments to initialize the data in the structures (similarity to what you would do when prompting from the user).<\/li>\n<li data-select-like-a-boss=\"1\">Using explicit initializing for the structure.<\/li>\n<li data-select-like-a-boss=\"1\">Using compound literal to initialize the structure (supports re-initializing later in the code).<\/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\">Example 1: Assignments<\/span><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">Example 2: Initializing<\/span><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">Example 3: Compound literal<\/span><\/div><div class=\"su-tabs-panes\"><div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"Example 1: Assignments\">\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=\"Example 2: Initializing\">\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=\"Example 3: Compound literal\">\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>When nesting structures, the compiler needs to recognize the struct type you intend to use as a member. In the labs, first the inner struct will be defined, followed by defining the outer struct that contains the previously defined struct. This principle is also followed in the examples below. Alternatively, a forward declaration can be &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/programming-ii\/code-samples\/substruct\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Nested structure 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":9,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/2552\/revisions"}],"predecessor-version":[{"id":11438,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/2552\/revisions\/11438"}],"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}]}}