ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

C Programming Tutorial• 7 min read

Structures (struct), Unions & Typedef

Custom compound data types, the arrow operator ->, and memory alignment

Structures (struct) group related variables of different data types into a single contiguous record, forming the basis of composite data modeling in C.

Struct Access and The Arrow Operator

Dot operator (.): Used when accessing fields on a direct struct value: user.age.

Arrow operator (->): Shorthand syntax when accessing fields through a struct pointer: ptr->age (equivalent to (*ptr).age).

typedef: Creates clean type aliases, eliminating the need to write struct User repeatedly.

Creating and Passing Struct Pointers
#include <stdio.h>

typedef struct {
    char name[32];
    int studentID;
    double gpa;
} Student;

void printStudent(const Student *s) {
    // Arrow operator accesses members through pointer
    printf("Student: %s (ID: %d) | GPA: %.2f\n", s->name, s->studentID, s->gpa);
}

int main(void) {
    Student s1 = {"Grace Hopper", 101, 3.95};
    printStudent(&s1); // Pass by pointer to avoid copying large struct
    return 0;
}
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Which operator is used in C to access a struct member when you have a pointer to that struct?