Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

C Structures struct, typedef, Nested Structs: Tutorial, Examples, FAQs & Interview Tips

What is a Structure?

A struct (structure) is a user-defined data type that groups variables of different types under a single name. Structures are ideal for representing real-world entities like students, employees, or products.

// Define a structure
struct Student {
    char name[50];
    int  age;
    float gpa;
};

// Declare a variable
struct Student s1;

// Access members with dot operator
s1.age = 20;
strcpy(s1.name, "Alice");

// Initialize at declaration
struct Student s2 = {"Bob", 22, 3.8f};

Pointer to Structure

When accessing structure members through a pointer, use the arrow operator -> instead of the dot operator.

struct Student *ptr = &s1;
printf("%s", ptr->name);   // same as (*ptr).name
Basic struct - Student Record
#include <stdio.h>
#include <string.h>

struct Student {
    char  name[50];
    int   age;
    float gpa;
    char  grade;
};

void printStudent(struct Student s) {
    printf("Name:  %s\n",  s.name);
    printf("Age:   %d\n",  s.age);
    printf("GPA:   %.2f\n", s.gpa);
    printf("Grade: %c\n",  s.grade);
}

int main() {
    struct Student s1;
    strcpy(s1.name, "Alice");
    s1.age   = 20;
    s1.gpa   = 3.85f;
    s1.grade = 'A';

    // Initialize at declaration
    struct Student s2 = {"Bob", 22, 3.2f, 'B'};

    printf("--- Student 1 ---\n");
    printStudent(s1);

    printf("\n--- Student 2 ---\n");
    printStudent(s2);

    printf("\nSize of struct Student: %zu bytes\n", sizeof(struct Student));

    return 0;
}
Array of Structs and Pointer to Struct
#include <stdio.h>
#include <string.h>

struct Employee {
    char name[50];
    int  id;
    float salary;
};

int main() {
    // Array of structures
    struct Employee team[3] = {
        {"Alice", 101, 75000.0f},
        {"Bob",   102, 68000.0f},
        {"Carol", 103, 82000.0f}
    };

    printf("%-10s %5s %10s\n", "Name", "ID", "Salary");
    printf("---------------------------\n");
    for (int i = 0; i < 3; i++) {
        printf("%-10s %5d %10.2f\n",
               team[i].name, team[i].id, team[i].salary);
    }

    // Pointer to struct - use arrow operator ->
    struct Employee *ptr = &team[0];
    printf("\nFirst employee via pointer:\n");
    printf("Name: %s, Salary: %.2f\n", ptr->name, ptr->salary);

    ptr++;  // move to next struct
    printf("Second employee via pointer:\n");
    printf("Name: %s, ID: %d\n", ptr->name, ptr->id);

    return 0;
}
typedef struct and Nested Struct
#include <stdio.h>
#include <string.h>

// typedef lets you use the type without 'struct' keyword
typedef struct {
    int day;
    int month;
    int year;
} Date;

typedef struct {
    char name[50];
    Date birthdate;   // nested struct
    float salary;
} Person;

int main() {
    Person p;
    strcpy(p.name, "Alice");
    p.birthdate.day   = 15;
    p.birthdate.month = 6;
    p.birthdate.year  = 1995;
    p.salary = 60000.0f;

    printf("Name:      %s\n", p.name);
    printf("Birthdate: %02d/%02d/%d\n",
           p.birthdate.day, p.birthdate.month, p.birthdate.year);
    printf("Salary:    %.2f\n", p.salary);

    // No need to write 'struct Person' - just 'Person'
    Person p2 = {"Bob", {20, 3, 1990}, 55000.0f};
    printf("\nName: %s, Born: %d\n", p2.name, p2.birthdate.year);

    return 0;
}

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.