Passing Pointers to Struct
Let's look at a couple of ways that are used for handling structs using pointers.
We'll cover the following...
Pointer to a struct
Pointers can also be used to point to a struct. Here is how this would be done:
Press + to interact
#include <stdio.h>#include <stdlib.h>typedef struct {int year;int month;int day;} date;int main(void) {date *today;today = malloc(sizeof(date));// the explicit way of accessing fields of our struct(*today).day = 21;(*today).month = 5;(*today).year = 2024;// the more readable shorthand way of doing ittoday->day = 21;today->month = 5;today->year = 2024;printf("The date is %d/%d/%d\n", today->day, today->month, today->year);free(today);return 0;}
...