Skip to main content
C Programming Basics
CHAPTER 15 Beginner

Structures in C

Updated: May 17, 2026
5 min read

# CHAPTER 15

Structures in C

1. Introduction

Arrays are great, but they can only store one type of data (e.g., an array of ONLY integers). What if you want to store a student's name (string), age (int), and GPA (float) together? In C, you use a Structure (struct) to create a custom data type that groups related variables of different types under a single name.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define and declare a structure.
  • Access and modify structure members using the dot (.) operator.
  • Create arrays of structures.
  • Use pointers with structures using the arrow (->) operator.
  • Build an Employee Management System.

3. Defining a Structure

You define a structure using the struct keyword, usually outside the main() function.
c
12345
struct Student {
    char name[50];
    int age;
    float gpa;
}; // Don't forget the semicolon!

*Note: This just creates a blueprint. It doesn't allocate memory until you create a variable of this type.*

4. Declaring and Accessing Structure Variables

Once defined, you create a variable of that structure type and access its members using the dot operator (.).
c
12345678910111213141516171819202122232425
#include <stdio.h>
#include <string.h>

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

int main() {
    // 1. Declare a structure variable
    struct Student s1;

    // 2. Assign values (Must use strcpy for strings!)
    strcpy(s1.name, "Alice");
    s1.age = 20;
    s1.gpa = 3.8;

    // 3. Access values
    printf("Name: %s\n", s1.name);
    printf("Age: %d\n", s1.age);
    printf("GPA: %.1f\n", s1.gpa);

    return 0;
}

5. Initializing Structures

You can initialize a structure at the time of declaration, similar to an array.
c
1
struct Student s2 = {"Bob", 22, 3.5};

6. Arrays of Structures

If you want to store 100 students, you don't create 100 variables. You create an array of your custom struct.
c
123456
struct Student class[3];

// Initialize first student
strcpy(class[0].name, "Charlie");
class[0].age = 21;
class[0].gpa = 4.0;

7. Pointers to Structures

When passing a structure to a function, it's more memory-efficient to pass a pointer. When accessing a structure's members via a pointer, you must use the arrow operator (->) instead of the dot operator.
c
123456
struct Student s3 = {"David", 19, 3.2};
struct Student *ptr = &s3;

// Using the arrow operator
printf("Name: %s\n", ptr->name); 
ptr->age = 20; // Updates David's age

8. Mini Project: Employee Management System

c
123456789101112131415161718192021222324252627282930
#include <stdio.h>

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

int main() {
    struct Employee emp[2]; // Array of 2 employees

    printf("=== EMPLOYEE ENTRY ===\n");
    for (int i = 0; i < 2; i++) {
        printf("\nEnter details for Employee %d:\n", i + 1);
        printf("ID: ");
        scanf("%d", &emp[i].id);
        printf("Name: ");
        scanf("%s", emp[i].name);
        printf("Salary: ");
        scanf("%f", &emp[i].salary);
    }

    printf("\n=== EMPLOYEE DATABASE ===\n");
    for (int i = 0; i < 2; i++) {
        printf("ID: %d | Name: %s | Salary: $%.2f\n", 
               emp[i].id, emp[i].name, emp[i].salary);
    }

    return 0;
}

9. Memory-Level Explanation (Struct Padding)

You might think sizeof(struct Student) equals the exact sum of its parts (50 bytes + 4 bytes + 4 bytes = 58 bytes). However, CPUs read memory in chunks (words). To optimize CPU reading speed, the C compiler adds empty bytes ("padding") between members to align them to 4-byte or 8-byte boundaries. So sizeof might return 60 bytes.

10. Common Mistakes

  • Forgetting the semicolon after the struct definition closing brace }.
  • Assigning strings directly: s1.name = "Alice"; is illegal in C. You must use strcpy(s1.name, "Alice");.
  • Using . with pointers: If ptr is a pointer, ptr.age is a syntax error. You must use ptr->age.

11. Exercises

  1. 1. Define a struct Point with x and y integer coordinates. Create two points and write a function to calculate the distance between them.
  1. 2. Create a struct Book (title, author, price). Initialize it and print it.

12. MCQ Quiz with Answers

Question 1

Which keyword is used to create a structure in C?

Question 2

How do you access a member of a structure variable?

Question 3

How do you access a member of a structure using a pointer?

Q4. Can a structure contain an array? a) Yes b) No Answer: a) Yes

Q5. Can a structure contain a function? a) Yes b) No, C structures only contain data (unlike C++ classes) Answer: b) No, C structures only contain data (unlike C++ classes)

Question 6

What happens if you forget the semicolon at the end of a struct definition?

Question 7

Is sizeof(struct) always exactly equal to the sum of the sizes of its members?

Question 8

How do you declare an array of 10 structures named Car?

Question 9

Which string function is used to assign a string to a structure member?

Question 10

Structures in C allow you to implement:

13. Interview Questions

  • Q: What is structure padding and packing in C?
  • Q: Differentiate between the dot (.) operator and the arrow (->) operator.
  • Q: Can you nest one structure inside another structure? (Yes).

14. Summary

Structures (struct) allow you to group related variables of different data types into a single custom type. You access members using the dot operator, and if accessing via a pointer, you use the arrow operator. Arrays of structures are incredibly useful for managing databases of records.

15. Next Chapter Recommendation

In Chapter 16: Unions and Enumerations, we will look at union, a memory-saving cousin to struct, and enum, which makes code more readable.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·