Skip to content
Learn Netverks

Lesson

Step 17/36 47% through track

structs-c

Structs

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Structs: the syntax, patterns, and safety habits you need before advancing in C.

Structs model data in kernels and embedded firmware—layout and alignment matter in production.

You will apply Structs in contexts like: Kernels, drivers, embedded devices, and performance libraries used by other languages.

Write C in main.c with int main(), click Run on server—the dev runner compiles with cc/gcc -std=c11 and runs the binary; read stderr for compile and linker errors (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

struct groups named fields into one value—C's primary way to build composite data without classes. Compare with Java POJOs or Rust structs, but without methods attached by default.

Definition and access

struct Point {
    int x;
    int y;
};

struct Point p = { .x = 3, .y = 4 };
printf("%d\n", p.x);

C99 designated initializers (.x = 3) clarify field assignment.

Pointers to structs

p->x is shorthand for (*p).x when p is a struct Point *.

Important interview questions and answers

  1. Q: struct size?
    A: At least sum of members but often larger due to alignment padding—use sizeof.
  2. Q: Copying structs?
    A: Assignment copies all bytes—fine for small structs; use pointers for large ones.

Self-check

  1. What operator accesses a field through a struct pointer?
  2. Why might sizeof(struct) exceed member sizes?

Interview prep

Struct padding?

Compiler inserts padding for alignment—sizeof(struct) may exceed sum of member sizes.

Arrow operator?

p->member is syntactic sugar for (*p).member when p is a struct pointer.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Padding why?
  • typedef use?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump