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 bracket array notation:
- In PrintEmployees1() , you could use pEmployees[i].member instead.
- In PrintEmployees3() , you could pass pEmployees[i] instead (but not pEmployee[i] ).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
/** * File: struct_pointers.c * Author: Risto Heinsar * Created: 25.02.2015 * Modified: 13.02.2024 * * Descriptions: A longer sample for passing structures to functions and * using pointers to access them. This code contains multiple * ways to achieve the same goal. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define N 20 typedef struct employee { int employeeCode; char fName[N]; char lName[N]; float wage; } employee; void PrintEmployee(employee *pEmployee); void PrintEmployees1(employee *pEmployees, int n); void PrintEmployees2(employee *pEmployees, int n); void PrintEmployees3(employee *pEmployees, int n); void PrintEmployees4(employee *pEmployees, int n); int main(void) { employee manager = {75, "Indrek", "Tamm", 12.75f}; employee staff[] = {{20, "Toomas", "Vali", 5.22f}, {23, "Madis", "Mets", 4.21f}, {25, "Kaspar", "Kuusk", 3.25f}}; // Prints a single employee records PrintEmployee(&manager); putchar('\n'); // Calculate number of employees in the array int n = sizeof(staff) / sizeof(employee); // 3 different ways of printing employee data PrintEmployees1(staff, n); PrintEmployees2(staff, n); PrintEmployees3(staff, n); PrintEmployees4(staff, n); return EXIT_SUCCESS; } /** * Description: Prints the records of a single employee * * Parameters: pEmployee - pointer to employee data * Return: none */ void PrintEmployee(employee *pEmployee) { printf("Employee %06d, %s %s makes %2.2f in an hour\n", pEmployee->employeeCode, pEmployee->fName, pEmployee->lName, pEmployee->wage); } /** * Description: This is an example print function that prints all employee data * using pointer arithmetic to index the struct array. Prinding is * done directly. * * Parameters: pEmployee - pointer to employee data * n - number of employees * Return: none */ void PrintEmployees1(employee *pEmployees, int n) { for (int i = 0; i < n; i++) { printf("Employee %06d, %s %s makes %2.2f in an hour\n", (pEmployees + i)->employeeCode, (pEmployees + i)->fName, (pEmployees + i)->lName, (pEmployees + i)->wage); } putchar('\n'); } /** * Description: This is very similar to PrintEmployees1(), but in this case * we are incrementing the the copy of the pointer address. * * Parameters: pEmployee - pointer to employee data * n - number of employees * Return: none */ void PrintEmployees2(employee *pEmployees, int n) { // Store pointer to the first member separately (for clarity) employee *pEmployee = pEmployees; for(int i = 0; i < n; i++) { /* Print the current employee */ printf("Employee %06d, %s %s makes %2.2f in an hour\n", pEmployee->employeeCode, pEmployee->fName, pEmployee->lName, pEmployee->wage); /* Move the employee pointer to the next struct member (employee) */ pEmployee++; } putchar('\n'); } /** * Description: This is very similar to PrintEmployees1(), but in this case * we are using an exisitng print function to handle printing. * Useuful if you need to print the same data in multiple places. * * Parameters: pEmployee - pointer to employee data * n - number of employees * Return: none */ void PrintEmployees3(employee *pEmployees, int n) { // Store pointer to the first member separately (for clarity) employee *pEmployee = pEmployees; for(int i = 0; i < n; i++) { /* Print the current employee */ PrintEmployee(pEmployee); /* Move the employee pointer to the next struct member (employee) */ pEmployee++; } putchar('\n'); } /** * Description: This is again the in same notion as the previous examples, * but this time we make a dedicated pointer to an employee * struct in the loop, offering even cleaner code * * Parameters: pEmployee - pointer to employee data * n - number of employees * Return: none */ void PrintEmployees4(employee *pEmployees, int n) { for(int i = 0; i < n; i++) { /* Select the employee from the struct pointer array */ employee *pEmployee = pEmployees + i; /* Print the current employee */ PrintEmployee(pEmployee); } putchar('\n'); } |