CBasicsbeginner

Structures in C with Simple Examples

Group related data with struct in C: a student record example, member access, array of structures, typedef, pointers with -> and structure vs array.

Published
TL;DR – Quick Answer

A structure in C is a user-defined type that groups related variables of different types under one name. You define it with the struct keyword, for example struct Student { int roll; char name[30]; float marks; };, create a variable such as struct Student s1;, and reach each member with the dot operator, as in s1.marks. An array of structures stores many such records.

On This Page

A student record has a roll number, a name and marks. These are three different types: an int, a string and a float. An array cannot hold them together because every element of an array must have the same type. A structure can.

This page builds a student record step by step: defining a structure, accessing its members, reading input, storing many records in an array of structures and passing a structure to a function. You should know strings in C first, because the name is a char array.

Defining a structure

struct Student {
    int roll;
    char name[30];
    float marks;
};
Part Meaning
struct Keyword that starts a structure definition
Student The structure's name, called its tag
roll, name, marks Members (also called fields)
; after } Required. Forgetting it is a common compile error

This definition is only a blueprint. It does not create any variable or use memory. Think of it as a blank admission form with three boxes.

Creating structure variables and accessing members

Create a variable with struct Student s1; and reach each member with the dot operator: s1.roll, s1.name, s1.marks.

#include <stdio.h>
#include <string.h>

struct Student {
    int roll;
    char name[30];
    float marks;
};

int main(void) {
    struct Student s1 = {101, "Anjali", 88.5f};
    struct Student s2;

    s2.roll = 102;
    strcpy(s2.name, "Kiran");
    s2.marks = 74.0f;

    printf("Roll: %d, Name: %s, Marks: %.1f\n", s1.roll, s1.name, s1.marks);
    printf("Roll: %d, Name: %s, Marks: %.1f\n", s2.roll, s2.name, s2.marks);
    return 0;
}

Output:

Roll: 101, Name: Anjali, Marks: 88.5
Roll: 102, Name: Kiran, Marks: 74.0

Two ways to fill a structure:

  • At declaration, with values in braces in the same order as the members: {101, "Anjali", 88.5f}.
  • Member by member, after declaration. Numbers use =, but the name is a char array, so it needs strcpy. Writing s2.name = "Kiran"; is a compile error.
  s1
  +--------+------------------+--------+
  | roll   | name             | marks  |
  | 101    | "Anjali"         | 88.5   |
  +--------+------------------+--------+

Reading a structure from the user

#include <stdio.h>

struct Student {
    int roll;
    char name[30];
    float marks;
};

int main(void) {
    struct Student s;

    printf("Enter roll, name and marks: ");
    scanf("%d %29s %f", &s.roll, s.name, &s.marks);

    printf("Student %d (%s) scored %.1f\n", s.roll, s.name, s.marks);
    return 0;
}

Sample run:

Enter roll, name and marks: 103 Ravi 91
Student 103 (Ravi) scored 91.0

&s.roll and &s.marks need &, like any int or float. s.name does not, because it is an array. %29s reads one word, so use fgets if names have spaces.

Array of structures

Real programs keep many records. An array of structures stores them all, and a loop processes them one by one.

#include <stdio.h>

struct Student {
    int roll;
    char name[30];
    float marks;
};

int main(void) {
    struct Student cls[3] = {
        {101, "Anjali", 88.5f},
        {102, "Kiran", 74.0f},
        {103, "Ravi", 91.0f}
    };
    int i, top = 0;
    float total = 0;

    printf("%-5s %-10s %s\n", "Roll", "Name", "Marks");
    for (i = 0; i < 3; i++) {
        printf("%-5d %-10s %.1f\n", cls[i].roll, cls[i].name, cls[i].marks);
        total += cls[i].marks;
        if (cls[i].marks > cls[top].marks) {
            top = i;
        }
    }

    printf("Average: %.2f\n", total / 3);
    printf("Topper: %s with %.1f\n", cls[top].name, cls[top].marks);
    return 0;
}

Output:

Roll  Name       Marks
101   Anjali     88.5
102   Kiran      74.0
103   Ravi       91.0
Average: 84.50
Topper: Ravi with 91.0

cls[i] is one whole student, and cls[i].marks is that student's marks. %-10s prints the name left-aligned in a space 10 characters wide, which keeps the columns neat. The variable top remembers the index of the highest marks seen so far.

typedef: a shorter name

Writing struct Student every time is long. typedef gives the type a new name.

#include <stdio.h>

typedef struct {
    char name[30];
    int age;
} Person;

int main(void) {
    Person p = {"Meena", 21};
    printf("%s is %d years old\n", p.name, p.age);
    return 0;
}

Output:

Meena is 21 years old

Now Person works like a built-in type name. Many C projects use this style.

Pointer to a structure and the arrow operator

When you have a pointer to a structure, use -> to reach a member. p->marks means the same as (*p).marks.

Passing a pointer to a function also lets the function change the original record, and it avoids copying the whole structure.

#include <stdio.h>

struct Student {
    int roll;
    char name[30];
    float marks;
};

void add_grace(struct Student *p, float grace) {
    p->marks = p->marks + grace;
}

void show(struct Student s) {
    printf("%s: %.1f\n", s.name, s.marks);
}

int main(void) {
    struct Student s = {102, "Kiran", 74.0f};

    show(s);
    add_grace(&s, 5.0f);
    show(s);
    return 0;
}

Output:

Kiran: 74.0
Kiran: 79.0
Function Receives Can change the original?
show(struct Student s) A copy of the structure No
add_grace(struct Student *p, ...) The address of the structure Yes, through p->marks

Pointers are explained with memory-box pictures in what is a pointer in C.

Nested structures

A member can itself be a structure.

#include <stdio.h>

struct Date {
    int day, month, year;
};

struct Employee {
    char name[30];
    struct Date joined;
};

int main(void) {
    struct Employee e = {"Suresh", {15, 7, 2025}};
    printf("%s joined on %02d/%02d/%d\n", e.name, e.joined.day, e.joined.month, e.joined.year);
    return 0;
}

Output:

Suresh joined on 15/07/2025

Use two dots to reach the inner member: e.joined.month. %02d pads a number to two digits with a leading zero.

Structure vs array

Point Array Structure
Types of values All the same type Can be different types
How you reach a value By index: marks[2] By name: s.marks
Defined with type name[size]; struct Tag { ... };
Assign one to another with = Not allowed Allowed: s2 = s1; copies every member
Compare with == Compares addresses, not contents Not allowed. Compare member by member
Typical use A list of marks One student record

The two work best together: an array of structures for a class of students, or a structure that contains an array, such as the name member.

Size of a structure

printf("%zu\n", sizeof(struct Student));

You might expect 4 + 30 + 4 = 38 bytes. On a typical 64-bit system with GCC this prints 40, because the compiler adds 2 unused bytes of padding after name so that marks starts at an address divisible by 4. Padding depends on the compiler and system, so always use sizeof rather than adding the sizes yourself.

Common mistakes with structures

Mistake Example Fix
Missing semicolon after } struct Student { ... } End the definition with };
Assigning a string member with = s.name = "Ravi"; strcpy(s.name, "Ravi");
Using . with a pointer p.marks where p is a pointer p->marks
Forgetting struct before the tag Student s; without a typedef struct Student s; or use typedef
Comparing structures with == if (s1 == s2) Compare each member
Wrong order in an initialiser {"Anjali", 101, 88.5f} Follow the member order: {101, "Anjali", 88.5f}

Quick revision

  1. struct groups members of different types under one name.
  2. The definition is a blueprint. Variables use memory.
  3. Reach members with . on a variable and -> on a pointer.
  4. Use strcpy for char array members.
  5. An array of structures stores many records: cls[i].marks.
  6. Pass a pointer to let a function change the original.

Structures are the base for linked lists and other data structures in C. In C++, a structure grows into a class that holds functions as well as data. See C++ classes and objects for the next step, or test yourself with the C programming interview questions.

Frequently Asked Questions

What is a structure in C in simple words?
A structure is a way to keep related information together. A student has a roll number, a name and marks. Instead of three separate variables, a struct Student holds all three in one variable.
What is the difference between a structure and an array?
An array holds many values of the same type, reached by index. A structure holds values that can be of different types, reached by member name. You can combine them, for example an array of structures.
What is the difference between the dot and arrow operators?
Use the dot operator with a structure variable, as in s.marks. Use the arrow operator with a pointer to a structure, as in p->marks, which is short for (*p).marks.
Does defining a structure use memory?
No. The struct definition is only a blueprint that describes the members. Memory is set aside when you create a variable of that structure type.
Why is sizeof a structure sometimes larger than the sum of its members?
The compiler may add unused bytes, called padding, between members so each member sits at an address the processor can read quickly. The exact size depends on the compiler and system, so use sizeof instead of adding sizes by hand.

Want to Build Your Career in Java Full Stack with AI?

Join CodeBegun and train with working industry engineers — See the Java Full Stack course in Hyderabad

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 5 December 2026 LinkedIn
Chat with us