TalentJR

⚙️ C programming basics · Class 10 · Lesson 4 of 18 · Free

Structures in C – Group Related Data with struct

⏱ About 35 minutes · ▶ run real C here — nothing to install · no sign-in

In this lesson you will

  • Define a struct, create variables of it and read or change its members
  • Build an array of structs and search it, like a class list
  • Pass structs to functions by value and by pointer, using the -> operator

A student record has a name, a roll number, marks and a percentage. Keeping four separate arrays in step is error-prone: sort one, forget another, and Riya gets Aman's marks. A struct keeps all the details of one student together as one value.

What is a struct in C?

A struct (structure) is your own data type made of several named parts called members. Each member can have a different type.

struct Student { ... }; only describes the shape. It is like a blank form. struct Student s1 = {...}; fills one form. Note the ; after the closing brace of the definition.

You read or change a member with a dot: s1.roll. For a char array member, use strcpy to change it (Lesson 3).

Try it: make a second student s2 with your own details and print them too.

⚙️ C — you can edit this code
Output
Name: Anjali Rao
Roll: 14
Percent: 88.5
Updated: Anjali R., 91.0%

What does typedef struct mean in C?

Writing struct Book every time is long. typedef gives the type a short name. After typedef struct { ... } Book; you simply write Book.

You can also fill members by name: { .title = "...", .copies = 3 }. This is clearer and the order does not matter. Members you skip become 0.

Unlike arrays, one struct can be copied into another with =. Every member is copied, even arrays inside it. The two copies are then separate.

Try it: change copy.copies to 0 and check that original still has 3 copies and its own title.

⚙️ C — you can edit this code
Output
Original: Panchatantra Tales by Vishnu Sharma, 3 copies
Copy:     Jataka Tales by Vishnu Sharma, 1 copies

How do you make an array of structures in C?

An array of structs is like a register: each row is one complete record. class10[i].name is the name in row i.

This lets you loop over records, add totals and find the best one, while each student's data stays together.

Try it: add a fifth student {"Meera", 5, 97.5} and change 4 to 5 everywhere it is the count.

⚙️ C — you can edit this code
Output
Riya   roll 1  91.4%
Aman   roll 2  76.0%
Zoya   roll 3  94.2%
Kabir  roll 4  68.8%
Class average: 82.60%
Topper: Zoya

💡 %-6s prints the name in a 6-wide column, lined up on the left.

How do you pass a struct to a function, and what does -> mean in C?

Passing a struct by value gives the function a full copy. That is fine for printing, but changes are lost, and copying big structs is slow.

To change the caller's struct, pass its address: pay_fee(&acc, 3000). Inside, acc is a pointer. acc->paid means (*acc).paid: follow the pointer, then take the member.

Use const for pointers you only read, like show(const FeeAccount *acc).

Try it: call pay_fee a third time with 4000 and run again.

⚙️ C — you can edit this code
Output
Rohan: paid Rs 0 of Rs 15000, due Rs 15000
Rohan: paid Rs 8000 of Rs 15000, due Rs 7000

Can a struct contain another struct in C?

Yes. A member can itself be a struct. This is called a nested struct. Here a library Issue has a Date inside it.

You reach inner members with two dots: issue.due.day. We read the issue date with scanf; "%d/%d/%d" means three numbers separated by slashes.

Try it: change the input to 25/12/2026. The simple due-date rule below does not handle month ends yet; can you add that?

⚙️ C — you can edit this code
Output
Issue date (dd/mm/yyyy): 05/08/2026
Wings of Fire is due back on 19/08/2026

💡 scanf returns how many values it filled. Checking it catches wrong input.

🤔 Predict the output

Think like C: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

struct Point { int x; int y; };
struct Point a = {1, 2};
struct Point b = a;
b.x = 10;
printf("%d %d\n", a.x, b.x);

Question 2. What does this print?

struct Marks { int maths; int science; };
struct Marks m = {70, 80};
struct Marks *p = &m;
p->maths += 5;
(*p).science -= 10;
printf("%d %d\n", m.maths, m.science);

Question 3. What does this print?

struct Item { char name[10]; int qty; };
struct Item shop[3] = {{"Rice", 5}, {"Dal", 0}, {"Salt", 2}};
int empty = 0;
for (int i = 0; i < 3; i++) {
    if (shop[i].qty == 0) {
        empty++;
    }
}
printf("%d %s\n", empty, shop[2].name);

🧩 Your challenge

A kirana shop keeps its stock as an array of Item structs (name, qty, price in Rs). Write a function stock_value() that takes a pointer to one Item and returns qty * price. Print every item with fewer than 5 in stock, then the total stock value. Expected output: Low stock: Sugar (3) Low stock: Ghee (2) Total stock value: Rs 2785

Expected output :
Low stock: Sugar (3)
Low stock: Ghee (2)
Total stock value: Rs 2785
⚙️ C — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

#include <stdio.h>

struct Student {
    char name[20];
    int roll;
}

int main(void) {
    struct Student s = {"Riya", 1};
    printf("%s %d\n", s.name, s.roll);
    return 0;
}

Line 6: error: expected ';' after struct

✅ Right

#include <stdio.h>

struct Student {
    char name[20];
    int roll;
};

int main(void) {
    struct Student s = {"Riya", 1};
    printf("%s %d\n", s.name, s.roll);
    return 0;
}

A struct definition must end with }; — the semicolon after the brace is easy to forget.

❌ Wrong

struct Fee { int paid; };
struct Fee f = {0};
struct Fee *p = &f;
p.paid = 500;
printf("%d\n", f.paid);

Line 4: error: member reference type 'struct Fee *' is a pointer; did you mean to use '->'?

✅ Right

struct Fee { int paid; };
struct Fee f = {0};
struct Fee *p = &f;
p->paid = 500;
printf("%d\n", f.paid);

p is a pointer, not a struct. Use -> with a pointer and . with a struct variable.

❌ Wrong

#include <stdio.h>

typedef struct {
    int paid;
} Fee;

void pay(Fee f, int amount) {
    f.paid += amount;
}

int main(void) {
    Fee rohan = {0};
    pay(rohan, 5000);
    printf("Paid: %d\n", rohan.paid);
    return 0;
}

Compiles and runs, but prints: Paid: 0 ⏎

✅ Right

#include <stdio.h>

typedef struct {
    int paid;
} Fee;

void pay(Fee *f, int amount) {
    f->paid += amount;
}

int main(void) {
    Fee rohan = {0};
    pay(&rohan, 5000);
    printf("Paid: %d\n", rohan.paid);
    return 0;
}

A struct passed by value is copied, so the function changes only its copy. Pass a pointer to change the original.

📌 Remember

  • A struct groups related members of different types into one new type.
  • Use . to reach a member of a struct variable and -> through a pointer to a struct.
  • typedef gives a short name; designated initializers like .copies = 3 make code clearer.
  • Structs can be copied with =, stored in arrays and nested inside other structs.
  • Pass a pointer to a struct when a function must change it (or to avoid a big copy).

Finished the quiz and the challenge?

Questions students ask

What is the difference between a struct and an array in C?

An array holds many values of the SAME type, reached by number (marks[2]). A struct holds values of DIFFERENT types, reached by name (s.roll). You often combine them: an array of structs.

What is the difference between . and -> in C?

Use a dot with a struct variable: s.roll. Use an arrow with a pointer to a struct: p->roll, which is short for (*p).roll.

Can I compare two structs with == in C?

No, C does not allow == on structs. Compare the members you care about one by one, for example a.roll == b.roll, and use strcmp for string members.

Is a struct in C the same as a class?

A struct only holds data. A class in C++ or Java also holds functions (methods) and can hide its data. In C, you write separate functions that take a pointer to the struct, which is a similar idea.