No history yet

Introduction to Structures

Grouping Related Data

So far, you've worked with basic data types like int, float, and char. You've also used arrays to store collections of a single data type, like a list of numbers or a series of characters. But what if you need to group different types of data that are all related to a single concept?

Imagine you want to store information about a student: their name (a string of characters), their student ID (an integer), and their GPA (a floating-point number). An array won't work here because it can only hold one type of data. You'd have to create separate variables, like studentName, studentID, and studentGPA. This approach can get messy and disorganized, especially if you have many students.

Structures solve this problem. They are custom data types that let you bundle together variables of different data types under a single name.

Defining a Structure

To create a structure, you use the struct keyword. Think of it as creating a blueprint for a new data type. You define the structure by listing the variables, called members or fields, that will be part of it. Each member has its own data type and name.

struct Student {
    char name[50];
    int id;
    float gpa;
}; // Don't forget the semicolon!

This code defines a new structure type named Student. It has three members: name (a character array to store a string), id (an integer), and gpa (a float).

It's important to understand that this definition is just a template. It doesn’t create any variables or allocate any memory yet. It just tells the compiler what a Student looks like.

Creating and Using Structures

Once you've defined the structure, you can declare variables of that type, just like you would with int or float. This is called creating an instance of the structure.

struct Student student1;

This line creates a variable named student1 of type struct Student. Now, the program allocates memory for student1 to hold a character array, an integer, and a float.

To access the members of a structure variable, you use the dot operator (.):

#include <string.h> // Required for strcpy

// Assigning values to members
strcpy(student1.name, "John Doe");
student1.id = 12345;
student1.gpa = 3.7;

Notice that we use strcpy() to assign a value to the name member, because name is a character array. You can't assign strings directly with the = operator in C.

You can also initialize a structure variable when you declare it, which is often more concise.

struct Student student2 = {"Jane Smith", 67890, 3.9};

The values in the curly braces {} are assigned to the members of the structure in the order they were defined: "Jane Smith" goes to name, 67890 goes to id, and 3.9 goes to gpa.

Structures vs Arrays

The main difference is straightforward. Arrays store multiple elements of the same data type. Structures store multiple elements of different data types.

Using structures makes your code cleaner and more intuitive. By grouping related data, you can treat a complex entity like a "student" as a single unit, which simplifies managing data in your programs.

FeatureArrayStructure
Data TypesAll elements must be the same type.Members can be of different types.
PurposeTo store a list of similar items.To group logically related data.
AccessBy index, e.g., myArray[0]By member name, e.g., myStruct.member
KeywordNone (uses [])struct

Now, let's test your understanding of these fundamental concepts.

Quiz Questions 1/5

What is the primary purpose of a struct in C?

Quiz Questions 2/5

Consider the following code:

struct Book {
  char title[50];
  int year;
};

Does this code block allocate memory for a Book?

By mastering structures, you've gained a powerful tool for organizing data in a logical and manageable way.