Structs in C
A struct groups related variables of different types into one named unit: struct Point { int x; int y; }; then struct Point p; holds both coordinates together. Unlike arrays, members keep their own names and types — p.x reads as documentation, not pointer arithmetic.
Members pack in declaration order (possibly with padding gaps for alignment — check with sizeof and offsetof), and struct variables copy member-wise on assignment. C structs carry no methods or inheritance; behavior lives in plain functions taking struct pointers, which keeps data and operations honestly separate.
Group related data
struct Point { int x; int y; }; declares the layout once — every Point variable then owns its own x and y.
Access with dot
p.x = 3; names the member directly — self-documenting access no comment could improve.
Assignment copies whole
q = p; copies every member — convenient for small structs, expensive (and shallow) for big ones with pointers.
Padding exists
The compiler may insert gaps for alignment, so sizeof(struct) often exceeds the member sum — order members largest-first to pack tightly.
#include <stdio.h> struct Point { int x; int y;}; int main(void) { struct Point p; p.x = 3; p.y = 4; printf("%d,%d\n", p.x, p.y); return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.