🟡 Intermediate  ·  Lesson 27

Structures (struct)

What is a Structure?

A structure (struct) is a user-defined data type that groups related variables of different data types under one name. Unlike arrays (which store same-type elements), structures can group int, float, char, etc. together.

Real-world analogy: A student record has: rollNo (int), name (string), marks (float), grade (char). A struct groups all these together.

C Language – Basic Struct
#include <stdio.h>
#include <string.h>

// Define the structure
struct Student {
    int   rollNo;
    char  name[50];
    float marks;
    char  grade;
};

int main() {
    // Declare and initialize
    struct Student s1;
    s1.rollNo = 101;
    strcpy(s1.name, "Rahul Sharma");
    s1.marks = 87.5f;
    s1.grade = 'A';

    // Access members with dot operator
    printf("Roll No : %d\n", s1.rollNo);
    printf("Name    : %s\n", s1.name);
    printf("Marks   : %.1f\n", s1.marks);
    printf("Grade   : %c\n", s1.grade);
    return 0;
}
Roll No : 101 Name : Rahul Sharma Marks : 87.5 Grade : A

Initialization Methods

C Language
// Method 1: Member-by-member
struct Student s1;
s1.rollNo = 101; strcpy(s1.name, "Rahul"); s1.marks = 85.0f;

// Method 2: At declaration (ordered)
struct Student s2 = {102, "Priya", 92.5f, 'A'};

// Method 3: Designated initializers (C99)
struct Student s3 = {.rollNo=103, .marks=78.0f, .grade='B'};

Array of Structures

C Language – Student Array
#include <stdio.h>
#include <string.h>
struct Student { int roll; char name[30]; float marks; };
int main() {
    struct Student class[3] = {
        {1, "Rahul",  85.0f},
        {2, "Priya",  92.0f},
        {3, "Aditya", 78.0f},
    };
    printf("%-5s %-10s %s\n", "Roll", "Name", "Marks");
    printf("----------------------------\n");
    for(int i=0; i<3; i++)
        printf("%-5d %-10s %.1f\n", class[i].roll, class[i].name, class[i].marks);
    return 0;
}
Roll Name Marks ---------------------------- 1 Rahul 85.0 2 Priya 92.0 3 Aditya 78.0

Nested Structures

C Language – Nested Struct
struct Date { int day, month, year; };
struct Employee {
    char        name[50];
    float       salary;
    struct Date dob;   // Nested structure
};
struct Employee e = {"Gagan", 50000.0f, {15, 8, 1995}};
printf("%s DOB: %d/%d/%d\n", e.name, e.dob.day, e.dob.month, e.dob.year);
Gagan DOB: 15/8/1995

Summary

  • struct groups related variables of different types under one name
  • Members accessed with dot operator: var.member
  • Pointer to struct uses arrow: ptr->member
  • Array of structs: struct Type arr[n];
  • Structs can be nested (struct inside struct)
  • Use typedef struct to avoid writing struct keyword every time

Frequently Asked Questions

What is a structure in C?
A structure is a user-defined type that groups several related variables — possibly of different types — under one name. For example a Student structure can hold a name, a roll number and marks together, letting you treat one student as a single unit.
How do you access members of a structure in C?
With a structure variable you use the dot operator, such as s.name. If you have a pointer to a structure, you use the arrow operator instead, such as p->name. Both reach a member; which one you use depends on whether you have a value or a pointer.
What is an array of structures?
An array of structures stores many structure values in a list, for example struct Student class[50]; to hold fifty students. You then access each one by index and its members with the dot operator, like class[i].marks, which is ideal for record lists.
What is a nested structure in C?
A nested structure is a structure that contains another structure as a member. For instance a Student structure could include a Date structure for the date of birth. You reach the inner members by chaining the dot operator, such as s.dob.year.
What is the difference between a structure and an array?
An array holds many items of the same type accessed by index, while a structure groups items that can be of different types accessed by name. You often combine them — an array of structures — to store many records that each have several fields.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.