Skip to main content

Lesson 1 of 5

struct - Declaration, Access & Member Initialization

A `struct` groups related variables of different types into a single named type. Members are accessed with `.` (dot) on a struct value or `->` (arrow) on a pointer to struct. Structs are **passed by value** to functions by default - a full copy is made. For large structs, always pass by pointer.

12 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

struct - All Access Patterns & Member Initialisation

struct - All Access Patterns & Member Initialisation
#include <stdio.h>
#include <string.h>

/* ── Basic struct definition ─────────────────────────────── */
typedef struct {
    int    id;
    char   name[64];
    float  score;
    int    active;
} Student;

/* Pass by pointer - no copy, const for read-only access */
void print_student(const Student *s) {
    printf("ID=%-3d  Name=%-20s  Score=%.1f  Active=%s
",
           s->id, s->name, s->score, s->active ? "yes" : "no");
}

/* Return a struct by value - small structs acceptable */
Student create_student(int id, const char *name, float score) {
    Student s;            /* uninitialized - set all fields explicitly */
    s.id     = id;
    s.active = 1;
    s.score  = score;
    strncpy(s.name, name, sizeof(s.name) - 1);
    s.name[sizeof(s.name) - 1] = '';
    return s;             /* copy returned - compiler often optimizes */
}

int main(void) {
    /* ── Initialisation at declaration ───────────────────── */
    Student a = { 1, "Alice", 89.5f, 1 };       /* positional */
    Student b = { .id=2, .name="Bob", .score=92.0f, .active=1 }; /* designated (C99) */
    Student c = create_student(3, "Carol", 77.3f);

    print_student(&a);
    print_student(&b);
    print_student(&c);

    /* ── Dot vs arrow ───────────────────────────────────────
       s.member   - when 's' IS a struct value
       p->member  - when 'p' IS a pointer to struct
       p->member  is exactly (*p).member - same thing        */
    Student *pa = &a;
    pa->score = 95.0f;    /* modify via pointer */
    printf("Updated: %s score=%.1f
", pa->name, pa->score);

    /* ── Array of structs - contiguous in memory ─────────── */
    Student roster[3] = {a, b, c};
    printf("
Roster:
");
    for (int i = 0; i < 3; i++) print_student(&roster[i]);

    /* ── Zero-initialise a struct ─────────────────────────── */
    Student empty = {0};  /* all fields set to 0/NULL/0.0   */
    printf("empty.id=%d  empty.score=%.1f
", empty.id, empty.score);

    return 0;
}

Common Mistakes

  • Not initialising all struct members - `Student s; s.id = 1;` - `s.name`, `s.score`, `s.active` contain garbage. Always initialise all fields or use `= {0}` to zero-initialise.
  • Using `.` on a pointer - `ptr.member` is a compile error when `ptr` is a `struct Foo*`. Use `ptr->member` or `(*ptr).member`.
  • Passing large structs by value - `void process(BigStruct s)` copies the entire struct onto the stack on every call. For structs > ~16 bytes, pass `const BigStruct *s` instead.

Tip

Tip

Practice struct Declaration Access Member Initialization in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Structs group heterogeneous variables into a contiguous composite data type

Practice Task

Note

Practice Task - (1) Write a working example of struct Declaration Access Member Initialization from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with struct Declaration Access Member Initialization is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Key Takeaways

  • A `struct` groups related variables of different types into a single named type.
  • Not initialising all struct members - `Student s; s.id = 1;` - `s.name`, `s.score`, `s.active` contain garbage. Always initialise all fields or use `= {0}` to zero-initialise.
  • Using `.` on a pointer - `ptr.member` is a compile error when `ptr` is a `struct Foo*`. Use `ptr->member` or `(*ptr).member`.
  • Passing large structs by value - `void process(BigStruct s)` copies the entire struct onto the stack on every call. For structs > ~16 bytes, pass `const BigStruct *s` instead.

Finished this lesson?

0 of 5 in Module 7
Sign in to save progress

Topics in This Module

0/5 done