{"id":2545,"date":"2016-02-15T14:21:04","date_gmt":"2016-02-15T12:21:04","guid":{"rendered":"http:\/\/blue.pri.ee\/ttu\/?page_id=2545"},"modified":"2025-12-10T13:00:26","modified_gmt":"2025-12-10T11:00:26","slug":"str_func_longer","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/programming-ii\/code-samples\/str_func_longer\/","title":{"rendered":"Structure pointer example"},"content":{"rendered":"<p>This example shows the use of pointers to structures and pointer arithmetic.<\/p>\n<p>The code includes 4 different ways of printing a struct array. All these ways give the same result. Which one to use depends on the needs as well as personal preferences and team requirements.<\/p>\n<p>Some notes for comparing this code to the square bracket array notation:<\/p>\n<ul>\n<li>In <span class=\"lang:default highlight:0 decode:true crayon-inline\">PrintEmployees1()<\/span>\u00a0, you could use <span class=\"lang:default highlight:0 decode:true crayon-inline \">pEmployees[i].member<\/span>\u00a0 instead.<\/li>\n<li>In <span class=\"lang:default highlight:0 decode:true crayon-inline\">PrintEmployees3()<\/span> , you could pass <span class=\"lang:default highlight:0 decode:true crayon-inline\">pEmployees[i]<\/span>\u00a0 instead (but not <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:        31.01.2025\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#include &lt;string.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\r\n    \/\/ Prints a single employee records\r\n    PrintEmployee(&amp;manager);\r\n    putchar('\\n');\r\n\r\n    \/\/ Calculate number of employees in the array\r\n    int n = sizeof(staff) \/ sizeof(Employee);\r\n    \r\n    \/\/ 3 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 employee data\r\n * Return:      none\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:  pEmployee - pointer to employee data\r\n *              n - number of employees\r\n * Return:      none\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:  pEmployee - pointer to employee data\r\n *              n - number of employees\r\n * Return:      none\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        \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:  pEmployee - pointer to employee data\r\n *              n - number of employees\r\n * Return:      none\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:  pEmployee - pointer to employee data\r\n *              n - number of employees\r\n * Return:      none\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>This example shows the use of pointers to structures and pointer arithmetic. The code includes 4 different ways of printing a struct array. All these ways give the same result. Which one to use depends on the needs as well as personal preferences and team requirements. Some notes for comparing this code to the square &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/programming-ii\/code-samples\/str_func_longer\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Structure pointer example<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2361,"menu_order":5,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width.php","meta":{"footnotes":""},"class_list":["post-2545","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/2545","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=2545"}],"version-history":[{"count":3,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/2545\/revisions"}],"predecessor-version":[{"id":11001,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/2545\/revisions\/11001"}],"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=2545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}