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 min•By Priygop Team•Updated 2026
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] = '