In this section, we will learn the use of pointers on structures in C.
Note: we’re assuming you already familiar with the structures in C Programming.
C Pointers to Struct
Just like the way we use pointers to basic-data types, we can use pointers to structure-types as well.
But the way we use pointers to access the fields of a structure is different from the usual way of using dot `.` operator.
In short, we use `->` operator (dash followed by the greater-than sign) in order to access the fields of a structure-variable.
Example: pointers to structs in C
#include <stdio.h> struct address{ char * city; char * street; int zip; }; struct employee { char *name ; char *lastName ; int id; struct address homeAddress; }; int main() { struct employee ellen = {.name="Ellen", .lastName = "Cordon", .id= 21, .homeAddress= { .city = "xyz", .street = "fff", .zip = 321333} }; struct employee * pEllen = &ellen; pEllen->id = 25; printf("%s %s %d %s %s %d \n",pEllen->name, pEllen->lastName,pEllen->id, pEllen->homeAddress.city, pEllen->homeAddress.street, pEllen->homeAddress.zip ); return 0; }
Output:
Ellen Cordon 25 xyz fff 321333
In this example, we assigned the address of the `ellen` variable to the `pEllen` which is a pointer of type `employee`.
Then via `->` operator we used the `pEllen` pointer to change the value assigned to the `id` field.
Also, in the call to the `printf()` function we used `pEllen` pointer and `->` operator to access the value of each field of `ellen` variable and sent them to the output stream.
Passing structure-variables to functions by address:
With the help of pointers, we can pass a structure-variable to a function as its argument by address. In such a case, the target function is capable of changing the content of the structure-variable and this change is visible to both the pointer and the structure-variable itself.
Note: if you want to know more about pointers, you can check the pointers section.
Example: passing structures to functions by address
#include <stdio.h> struct address{ char * city; char * street; int zip; }; struct employee { char *name ; char *lastName ; int id; struct address homeAddress; }; void changeContent (struct employee *); int main() { struct employee ellen = {.name="Ellen", .lastName = "Cordon", .id= 21, .homeAddress= { .city = "xyz", .street = "fff", .zip = 321333} }; changeContent(&ellen); printf("The employee's id is: %d \n",ellen.id ); return 0; } void changeContent(struct employee * emp){ emp->id = 50; }
Output:
The employee's id is: 50
Here in this example, the `changeContent()` function accepts one argument and that is a pointer to any variable of type `employee-structure`.
So we can pass the address of the `ellen` variable because this variable is of type `employee`.
Also inside the body of the `changeContent()` function, we changed the value of the `id` field and so this change is also visible when we called the `id` field via the `ellen` variable in the `printf()` function.
C Struct: Dynamic Memory Allocation
As mentioned in `malloc()` and `calloc()` sections, we can dynamically allocate memory to basic-type variables. The same is also possible for structure-type variables as well.
Example: dynamically allocate memory for structures in C
#include <stdio.h> #include <stdlib.h> struct address{ char * city; char * street; int zip; }; struct employee { char *name ; char *lastName ; int id; struct address homeAddress; }; int main() { struct employee * pEmp = (struct employee *) malloc(sizeof(struct employee)); pEmp->id = 10; pEmp->lastName = "Kimber"; pEmp->name = "Bauer"; pEmp->homeAddress.zip = 111111; pEmp->homeAddress.street= "sssss"; pEmp->homeAddress.city = "CCC"; printf("id: %d name: %s last name: %s\n", pEmp->id , pEmp->name, pEmp->lastName); return 0; }
Output:
id: 10 name: Bauer last name: Kimber
The important part of this example is the call to the sizeof operator as ` sizeof(struct employee)`. The return value of calling to the `sizeof()` operator here in this example is the sum of bytes that each field of the `employee` structure takes.
After the call to the `malloc()` function, the returned pointer is casted to the `struct employee *` and the `pEmp` variable took the address of this allocated memory space.
The rest of codes are exactly the same as if we assigned the address of a `structure-variable` to a pointer.