Structures
10 min lesson
- A structure (struct) groups several related variables of different types under one name, so they travel together as a single unit.
- Structures matter because real things have many properties: a student has a name, an age and a grade, and keeping them together makes code clearer.
- Think of a struct as a form or a file card: one card holds several fields, like name, age and grade, all for one person.
- A member (or field) is one of the variables inside the struct, like name or age.
- You define a struct with the struct keyword and a list of members, then declare variables of that type.
- You access a member with the dot operator: student.age = 20;.
- The computer lays out the members in memory in order, possibly with padding between them so each is aligned for fast access.
- The syntax is: struct Name { type member1; type member2; }; then struct Name var;.
- A realistic use: storing a product with a name, price and stock count, or a point with x and y coordinates.
- A surprising detail: structs can be copied with = as a whole, and passing a struct to a function copies every member.
- The example program defines a struct for a student, fills in its fields, prints them, and copies it to another variable.
- Group related data in a struct instead of many loose variables.
- Use the dot operator to read or write members.
- Remember that assigning one struct to another copies all members.
- Pass structs by pointer when they are large to avoid copying.
- Structs are the foundation of real-world data modeling. Excellent work!
#include <stdio.h>
// Define a struct type that groups a student's data
struct Student {
char name[20];
int age;
double grade;
};
int main(void) {
// Declare a Student variable and fill its members
struct Student alice;
// Copy a name into the char array member
snprintf(alice.name, sizeof(alice.name), "Alice");
// Set the age member
alice.age = 20;
// Set the grade member
alice.grade = 91.5;
// Print each member with a label
printf("Name: %s\n", alice.name);
printf("Age: %d\n", alice.age);
printf("Grade: %.1f\n", alice.grade);
// Copy the whole struct into another variable
struct Student bob = alice;
// Change only bob's name to show they are separate copies
snprintf(bob.name, sizeof(bob.name), "Bob");
printf("Bob's grade: %.1f\n", bob.grade);
return 0;
}What happens: Name: Alice Age: 20 Grade: 91.5 Bob's grade: 91.5
Breaking it down
Struct definition
struct Student { char name[20]; int age; double grade; };Describes the shape of the data. It does not create a variable yet; it only defines a new type.
Declaring a variable
struct Student alice;Creates one Student in memory with all three members. The members start uninitialized, so fill them before reading.
Setting a member
alice.age = 20;Uses the dot operator to write to the age member of alice. Other members are unchanged.
Copying a struct
struct Student bob = alice;Copies every member of alice into bob. The two are independent afterward, so changing bob does not affect alice.
Printing members
printf("%s", alice.name);Reads the name member with the dot operator. Each member needs the format specifier that matches its type.
Common struct operations
| Operation | Syntax | Meaning |
|---|---|---|
| Define | struct S { int x; }; | Create a new struct type |
| Declare | struct S a; | Create a variable of that type |
| Access | a.x | Read or write a member |
| Copy | struct S b = a; | Copy all members |
| Pointer access | p->x | Access a member through a pointer |
A helpful analogy
A struct is like a file card in a filing cabinet. One card has several fields: a name, an age and a grade, all for one person. The card travels as one unit, and you read or write each field by its label.
Nesting rules
- 1.Define the struct before you declare variables of that type.
- 2.Use the dot operator for struct variables and the arrow operator -> for struct pointers.
- 3.Struct assignment copies all members; it does not share them.
- 4.Use snprintf or strncpy to fill char array members safely, not strcpy without a size limit.
Common mistakes
Forgetting the semicolon after the struct definition
Avoid
struct S { int x; }Use instead
struct S { int x; };The struct definition is a statement and needs a semicolon at the end. Without it the compiler reports errors on the next line.
Using -> on a struct variable
Avoid
alice->age = 20; // alice is not a pointerUse instead
alice.age = 20;The arrow operator is for pointers to structs. For a plain struct variable, use the dot operator.
Comparing structs with ==
Avoid
if (a == b) // compile errorUse instead
if (memcmp(&a, &b, sizeof a) == 0)C does not allow == on whole structs. Compare members one by one, or use memcmp with care about padding bytes.
Quick tips
- •Name struct types with a capital letter, like struct Student, so they stand out.
- •Use a pointer to a struct when passing to functions to avoid copying large data.
- •Print each member with a label so output is easy to read.
- •Try changing alice's grade and copying again to see the copy behavior.
- •Keep struct definitions in header files when several .c files need them.