{"id":4910,"date":"2020-02-17T22:42:02","date_gmt":"2020-02-17T20:42:02","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?page_id=4910"},"modified":"2026-01-26T17:04:49","modified_gmt":"2026-01-26T15:04:49","slug":"structure-array-example","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/programming-ii\/code-samples\/structure-array-example\/","title":{"rendered":"Structure array example"},"content":{"rendered":"<p>In the following example, we create an array of structs of type Student. There is no explicit use of pointers in the examples. Struct members are accessed using the dot notation for the struct array.<\/p>\n<pre class=\"toolbar:2 lang:c decode:true \">\/**\r\n * File:        struct_students.c\r\n * Author:      Risto Heinsar\r\n * Created:     10.01.2021\r\n * Last edit:   26.01.2026\r\n *\r\n * Description: Collects student data and finds the best student. Assumes\r\n *              there can only be one best student.\r\n *              \r\n *              Compile using --std=c99 or later.\r\n *\/\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\n#define LEN_NAME 32\r\n#define MAX_STUDENTS 8\r\n\r\nstruct Student\r\n{\r\n    char name[LEN_NAME];\r\n    float avgGrade;\r\n};\r\n\r\nint GetStudentCount(int min, int max);\r\nvoid ReadStudents(struct Student pupils[], int n);\r\nint GetBestStudentIndex(struct Student pupils[], int n);\r\n\r\nint main(void)\r\n{\r\n    struct Student students[MAX_STUDENTS];\r\n\r\n    \/* Get the number of students and store it into memory *\/\r\n    int numOfStudents = GetStudentCount(1, MAX_STUDENTS);\r\n    ReadStudents(students, numOfStudents);\r\n    \r\n    \/* Get the best sudent ID *\/\r\n    int bestStudentId = GetBestStudentIndex(students, numOfStudents);\r\n\r\n    printf(\"The best student is: %s with a grade of %.2f\\n\",\r\n                                           students[bestStudentId].name, \r\n                                           students[bestStudentId].avgGrade);\r\n    return EXIT_SUCCESS;\r\n}\r\n\r\n\/**\r\n * Description: Reads the number of students, loops until input within allowed\r\n *              range.\r\n * \r\n * Parameters:  min - lower bound, inclusive\r\n *              max - upper bound, inclusive\r\n * \r\n * Return:      -\r\n *\/\r\nint GetStudentCount(int min, int max)\r\n{\r\n    int num;\r\n    do\r\n    {\r\n        printf(\"Enter number of students (%d .. %d): \", min, max);\r\n        scanf(\"%d\", &amp;num);\r\n    }\r\n    while (num &lt; min || num &gt; max);\r\n    return num;\r\n}\r\n\r\n\/**\r\n * Description: Reads students information (name, grade)\r\n * \r\n * Parameters:  pupils - student array to be populated\r\n *              n - number of students\r\n * \r\n * Return:      -\r\n *\/\r\nvoid ReadStudents(struct Student pupils[], int n)\r\n{\r\n    for (int i = 0; i &lt; n; i++)\r\n    {\r\n        printf(\"Enter student's name and average grade (%d \/ %d)\\n\", i + 1, n);\r\n        scanf(\"%s %f\", pupils[i].name, &amp;pupils[i].avgGrade);\r\n    }\r\n}\r\n\r\n\/**\r\n * Description: Find the index of the student with the highest grade. Assumes\r\n *              only one maximum. \r\n * \r\n * Parameters:  pupils - student array to be populated\r\n *              n - number of students\r\n * \r\n * Return:      index of the student with the highest grade\r\n *\/\r\nint GetBestStudentIndex(struct Student pupils[], int n)\r\n{\r\n    int bestId = 0;\r\n    for (int i = 1; i &lt; n; i++)\r\n    {\r\n        if (pupils[bestId].avgGrade &lt; pupils[i].avgGrade)\r\n        {\r\n            bestId = i;\r\n        }\r\n    }\r\n    return bestId;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the following example, we create an array of structs of type Student. There is no explicit use of pointers in the examples. Struct members are accessed using the dot notation for the struct array. \/** * File: struct_students.c * Author: Risto Heinsar * Created: 10.01.2021 * Last edit: 26.01.2026 * * Description: Collects student &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/programming-ii\/code-samples\/structure-array-example\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Structure array example<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2361,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width-wide.php","meta":{"footnotes":""},"class_list":["post-4910","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/4910","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=4910"}],"version-history":[{"count":4,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/4910\/revisions"}],"predecessor-version":[{"id":11156,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/4910\/revisions\/11156"}],"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=4910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}