{"id":836,"date":"2015-02-25T00:29:05","date_gmt":"2015-02-24T22:29:05","guid":{"rendered":"http:\/\/www.blue.pri.ee\/ttu\/?page_id=836"},"modified":"2026-05-29T17:09:59","modified_gmt":"2026-05-29T15:09:59","slug":"strpikem-c","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/programmeerimine-ii\/koodinaited\/strpikem-c\/","title":{"rendered":"Struktuuriviida n\u00e4ited"},"content":{"rendered":"<p>Antud n\u00e4ites on kajastatud erinevaid viise kuidas k\u00e4sitleda viita struktuurile ning kasutada viidaaritmeetikat struktuuridest koosneva massiivi indekseerimiseks<\/p>\n<p>Kokku on koodis 4 erinevat n\u00e4idet kuidas tr\u00fckkida v\u00e4lja struktuuridest koosnevat massiivi. See millist kasutada v\u00f5ib s\u00f5ltuda nii eesm\u00e4rgist, kui ka enda eelistustest ja meeskonna koodin\u00f5uetest.<\/p>\n<p>Meeldetuletuseks &#8211; kui sul on viit struktuuridest koosnevale massiivile, siis saad kasutada ka kantsulgusid. N\u00e4iteks<\/p>\n<ul>\n<li><span class=\"lang:default highlight:0 decode:true crayon-inline\">PrintEmployees1()<\/span>\u00a0 funktsioonis saaks kasutada <span class=\"lang:default highlight:0 decode:true crayon-inline\">pEmployees[i].member<\/span> .<\/li>\n<li><span class=\"lang:default highlight:0 decode:true crayon-inline\">PrintEmployees3()<\/span>\u00a0 funktsioonis saaks kasutada <span class=\"lang:default highlight:0 decode:true crayon-inline\">pEmployees[i]<\/span>\u00a0 (kuid mitte <span class=\"lang:default highlight:0 decode:true crayon-inline \">pEmployee[i]<\/span> !).<\/li>\n<\/ul>\n<pre class=\"lang:c decode:true \">\/**\r\n * File:            struct_pointers.c\r\n * Author:          Risto Heinsar\r\n * Created:         25.02.2015\r\n * Modified:        29.05.2026\r\n *\r\n * Descriptions:    A longer sample for passing structures to functions and\r\n *                  using pointers to access them. This code contains multiple\r\n *                  ways to achieve the same goal.\r\n *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\n#define N 20\r\n\r\ntypedef struct Employee\r\n{\r\n    int employeeCode;\r\n    char fName[N];\r\n    char lName[N];\r\n    float wage;\r\n} Employee;\r\n\r\nvoid PrintEmployee(Employee *pEmployee);\r\nvoid PrintEmployees1(Employee *pEmployees, int n);\r\nvoid PrintEmployees2(Employee *pEmployees, int n);\r\nvoid PrintEmployees3(Employee *pEmployees, int n);\r\nvoid PrintEmployees4(Employee *pEmployees, int n);\r\n\r\nint main(void)\r\n{\r\n    Employee manager = {75, \"Indrek\", \"Tamm\", 12.75f};\r\n    Employee staff[] = {{20, \"Toomas\", \"Vali\",  5.22f},\r\n                        {23, \"Madis\",  \"Mets\",  4.21f},\r\n                        {25, \"Kaspar\", \"Kuusk\", 3.25f}};\r\n    \r\n    \/\/ Calculate number of employees in the array\r\n    int n = sizeof(staff) \/ sizeof(Employee);\r\n\r\n    \/\/ Prints a single employee records\r\n    PrintEmployee(&amp;manager);\r\n    putchar('\\n');\r\n    \r\n    \/\/ Three different ways of printing employee data\r\n    PrintEmployees1(staff, n);\r\n    PrintEmployees2(staff, n);\r\n    PrintEmployees3(staff, n);\r\n    PrintEmployees4(staff, n);\r\n\r\n    return EXIT_SUCCESS;\r\n}\r\n\r\n\r\n\/**\r\n * Description: Prints the records of a single employee\r\n * \r\n * Parameters:  pEmployee - pointer to single employee data\r\n * \r\n * Return:      -\r\n *\/\r\nvoid PrintEmployee(Employee *pEmployee)\r\n{\r\n    printf(\"Employee %06d, %s %s makes %2.2f in an hour\\n\", \r\n                                pEmployee-&gt;employeeCode, \r\n                                pEmployee-&gt;fName, \r\n                                pEmployee-&gt;lName,\r\n                                pEmployee-&gt;wage);\r\n}\r\n\r\n\r\n\/**\r\n * Description: This is an example print function that prints all employee data\r\n *              using pointer arithmetic to index the struct array. Prinding is\r\n *              done directly.\r\n * \r\n * Parameters:  pEmployees - pointer to employee data\r\n *              n - number of employees\r\n * \r\n * Return:      -\r\n *\/\r\nvoid PrintEmployees1(Employee *pEmployees, int n)\r\n{\r\n    for (int i = 0; i &lt; n; i++)\r\n    {\r\n        printf(\"Employee %06d, %s %s makes %2.2f in an hour\\n\", \r\n                                (pEmployees + i)-&gt;employeeCode, \r\n                                (pEmployees + i)-&gt;fName,\r\n                                (pEmployees + i)-&gt;lName, \r\n                                (pEmployees + i)-&gt;wage);\r\n    }\r\n    putchar('\\n');        \r\n}\r\n\r\n\r\n\/**\r\n * Description: This is very similar to PrintEmployees1(), but in this case \r\n *              we are incrementing the the copy of the pointer address.\r\n * \r\n * Parameters:  pEmployees - pointer to employee data\r\n *              n - number of employees\r\n * \r\n * Return:      -\r\n *\/\r\nvoid PrintEmployees2(Employee *pEmployees, int n)\r\n{\r\n    \/\/ Store pointer to the first member separately (for clarity)\r\n    Employee *pEmployee = pEmployees;\r\n    \r\n    for (int i = 0; i &lt; n; i++)\r\n    {\r\n       \/* Print the current employee *\/\r\n        printf(\"Employee %06d, %s %s makes %2.2f in an hour\\n\", \r\n                                    pEmployee-&gt;employeeCode, \r\n                                    pEmployee-&gt;fName,\r\n                                    pEmployee-&gt;lName, \r\n                                    pEmployee-&gt;wage);\r\n                \r\n        \/* Move the employee pointer to the next struct member (employee) *\/\r\n        pEmployee++;\r\n    }\r\n    putchar('\\n');\r\n}\r\n\r\n\r\n\/**\r\n * Description: This is very similar to PrintEmployees1(), but in this case \r\n *              we are using an exisitng print function to handle printing.\r\n *              Useuful if you need to print the same data in multiple places.\r\n * \r\n * Parameters:  pEmployees - pointer to employee data\r\n *              n - number of employees\r\n * \r\n * Return:      -\r\n *\/\r\nvoid PrintEmployees3(Employee *pEmployees, int n)\r\n{\r\n    \/\/ Store pointer to the first member separately (for clarity)\r\n    Employee *pEmployee = pEmployees;\r\n    \r\n    for (int i = 0; i &lt; n; i++)\r\n    {\r\n        \/* Print the current employee *\/\r\n        PrintEmployee(pEmployee);\r\n                \r\n        \/* Move the employee pointer to the next struct member (employee) *\/\r\n        pEmployee++;\r\n    }\r\n    putchar('\\n');\r\n}\r\n\r\n\/**\r\n * Description: This is again the in same notion as the previous examples,\r\n *              but this time we make a dedicated pointer to an employee\r\n *              struct in the loop, offering even cleaner code\r\n * \r\n * Parameters:  pEmployees - pointer to employee data\r\n *              n - number of employees\r\n * \r\n * Return:      -\r\n *\/\r\nvoid PrintEmployees4(Employee *pEmployees, int n)\r\n{\r\n    for (int i = 0; i &lt; n; i++)\r\n    {\r\n        \/* Select the employee from the struct pointer array *\/\r\n        Employee *pEmployee = pEmployees + i;\r\n        \r\n        \/* Print the current employee *\/\r\n        PrintEmployee(pEmployee);\r\n    }\r\n    putchar('\\n');\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Antud n\u00e4ites on kajastatud erinevaid viise kuidas k\u00e4sitleda viita struktuurile ning kasutada viidaaritmeetikat struktuuridest koosneva massiivi indekseerimiseks Kokku on koodis 4 erinevat n\u00e4idet kuidas tr\u00fckkida v\u00e4lja struktuuridest koosnevat massiivi. See millist kasutada v\u00f5ib s\u00f5ltuda nii eesm\u00e4rgist, kui ka enda eelistustest ja meeskonna koodin\u00f5uetest. Meeldetuletuseks &#8211; kui sul on viit struktuuridest koosnevale massiivile, siis saad kasutada &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/programmeerimine-ii\/koodinaited\/strpikem-c\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Struktuuriviida n\u00e4ited<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":684,"menu_order":5,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width.php","meta":{"footnotes":""},"class_list":["post-836","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/836","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=836"}],"version-history":[{"count":6,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/836\/revisions"}],"predecessor-version":[{"id":11428,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/836\/revisions\/11428"}],"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=836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}