In Lesson 8 of **Learn C With Me**, we dive into **structs in C** — a way to group multiple pieces of related data, potentially of different types, into a single user-defined type.
Using **Beej's Guide to C** as our study material, we start with the basics of declaring a `struct` and gradually work our way through initialization, pointers to structs, passing structs to functions, copying them, and comparing them.
We cover:
* What a **`struct`** is and why it is useful
* Creating a user-defined type with `struct`
* Declaring variables of a struct type
* Accessing fields with the **dot operator (`.`)**
* **Struct initializers**
* Initializing fields in declaration order
* **Designated initializers**
* Why designated initializers are safer when field order changes
* What happens to fields that aren't explicitly initialized
* **Passing structs to functions**
* Passing a complete struct versus passing a **pointer to a struct**
* Why passing a pointer can avoid unnecessary copying
* Modifying the original struct from inside a function
* Function declarations and why the compiler needs to know about a function before it is called
* Dereferencing a pointer to a struct
* The **arrow operator (`→`)**
* Why `ptr→field` is equivalent to `(*ptr).field`
* When to use `.` and when to use `→`
* **Copying structs**
* Returning structs from functions
* The difference between a **shallow copy** and a **deep copy**
* Why copying a struct also copies any pointer values it contains
* **Comparing structs**
* Why the safest approach is to compare individual fields one by one
One of the most useful parts of this lesson is seeing why **pointers to structs** are so common in C. Passing an entire struct to a function makes a copy, while passing a pointer allows the function to work with the original object and can also be much cheaper when the struct is large.
We also meet the **arrow operator** for the first time. Instead of writing something like `(*car).price`, C lets us write the much cleaner `car→price`. The two forms mean the same thing — the arrow is simply convenient syntax for accessing a field through a pointer to a struct.
Later, we look at copying structs and discover that a normal assignment performs a **shallow copy**. The fields themselves are copied, including pointer values, but the data those pointers refer to is not duplicated automatically. C doesn't provide built-in deep-copy behavior for structs.
Finally, we look at comparing structs and learn that there isn't a general-purpose struct equality operator in C. The safe approach is to **compare the relevant fields individually**, rather than assuming the raw memory representation will always be suitable for comparison.
As usual, we experiment with the examples, hit compiler errors, stop when something doesn't make sense, work through it with the live chat, and try to understand **what C is actually doing under the hood** rather than just memorizing syntax.
This is an **interactive, follow-along live course**. I am learning C alongside you, so expect questions, mistakes, experiments and plenty of discussion. No passive watching here — participate in the chat and work through the examples with us.
📅 **We go live every Saturday at 19:00 CEST** — interactive, follow-along C programming sessions.
📖 **Study material:**
[Beej's Guide to C](https://beej.us/guide/bgc/)
📚 **Course materials:**
[Linux Renaissance C Programming Course](https://linuxrenaissance.com/courses)
💬 **Homework & discussion:**
[Linux Renaissance Forum](https://forum.linuxrenaissance.com/c/programming/13)
📺 **All C Programming lessons:**
[C Programming playlist](https://www.youtube.com/playlist?list=PLE6A_ADcHGpc)
🔴 **This stream is proudly sponsored by OPNsense** — the open source firewall and routing platform trusted by professionals worldwide.
[OPNsense](https://opnsense.org)
### Support Linux Renaissance
🛒 Shop: [Linux Renaissance Shop](https://shop.linuxrenaissance.com)
☕ Ko-fi: [Ko-fi](https://ko-fi.com/darth)
🎖️ Patreon: [Patreon](https://www.patreon.com/c/linuxrenaissance/membership)
▶️ YouTube membership: [YouTube Membership](https://youtube.com/@LinuxRenaissance/join)
### Find me elsewhere
🌐 Blog: [Linux Renaissance](https://linuxrenaissance.com)
🎥 PeerTube: [Linux Renaissance on PeerTube](https://watch.linuxrenaissance.com)
📺 Odysee: [Linux Renaissance on Odysee](https://odysee.com/@LinuxRenaissance:1)
🎮 Twitch: [Linux Renaissance on Twitch](https://twitch.tv/LinuxRenaissance)
💬 Matrix: [Linux Renaissance Matrix space](https://matrix.to/#/%23space%3Afosshq.org)
Learn C With Me #08 — Structs | Live
Submitted 1 week ago by
darth@watch.linuxrenaissance.com to linuxrenaissance@watch.linuxrenaissance.com
https://watch.linuxrenaissance.com/w/fBkiXbYxB8cyS5CmRyHKYC