Introduction

Learn how to declare and use structures in your program.

We'll cover the following

Definition

A structure is a collection of variables of the same or different types under a single name.

In real life, we usually deal with entities that are collections of things, each thing having its own attributes. For example, a book is an entity with a collection of things, such as title, author, call number, publisher, number of pages, date of publication, etc.

All this data is dissimilar — the author is a string, whereas the number of pages is an integer. For dealing with such data, C provides a structure data type.

Structures can be declared in the following manner:

struct structure_Name 
{
    dataType member1;
    dataType member2;
};

Structures can be used in the following manner:

struct structure_Name s1;
s1.member1;
s2.member2;

What to expect

This chapter presents numerous detailed examples on structures that will allow you to understand this important part of C programming.

Get hands-on with 1400+ tech skills courses.