In C, arrays allow you to group together multiple variables of the same data type. However, real-world data is rarely homogeneous. For instance, if you want to store information about a student, you might need their name (a string), their age (an integer), and their GPA (a float).
This is where Structures, or structs, come into play. A structure is a user-defined data type in C that allows you to combine data items of different kinds. This comprehensive guide will show you how to declare, use, and master structures in C, ensuring your codebase is modular and highly readable.
You define a structure using the struct keyword. The definition acts as a blueprint. It does not allocate any memory itself; it merely tells the compiler what the new data type looks like.
struct Student {
char name[50];
int age;
float gpa;
};
Here, Student is the structure tag. The variables name, age, and gpa inside the structure are called members or fields.
Once you have defined the blueprint, you can create actual variables (instances) of that structure type.
#include <stdio.h> #include <string.h>// Define the blueprint struct Student { char name[50]; int age; float gpa; };
int main() { // Declare a variable of type 'struct Student' struct Student student1; // You can also initialize a structure at the time of declaration struct Student student2 = {"Alice Smith", 20, 3.8}; return 0; }
To access or modify the individual members of a structure variable, you use the dot operator (.).
#include <stdio.h> #include <string.h>struct Student { char name[50]; int age; float gpa; };
int main() { struct Student s1; // Assigning values using the dot operator // Note: We use strcpy for strings, not the '=' operator strcpy(s1.name, "John Doe"); s1.age = 22; s1.gpa = 3.5; // Printing structure members printf("Student Name: %s\n", s1.name); printf("Student Age: %d\n", s1.age); printf("Student GPA: %.1f\n", s1.gpa); return 0; }
Just like you can have an array of integers, you can have an array of structures. This is incredibly useful for managing lists of objects, like a database of employees or a roster of students.
#include <stdio.h>struct Point { int x; int y; };
int main() { // An array that holds 3 Point structures struct Point coordinates[3]; // Initializing the first point coordinates[0].x = 10; coordinates[0].y = 20; // Initializing the second point coordinates[1].x = 5; coordinates[1].y = 15; printf("Point 1: (%d, %d)\n", coordinates[0].x, coordinates[0].y); printf("Point 2: (%d, %d)\n", coordinates[1].x, coordinates[1].y); return 0; }
-> Operator)When working with structures dynamically allocated in memory or passing them to functions efficiently, you will use pointers to structures.
If you have a pointer to a structure, you cannot use the dot operator directly. Instead, you must use the arrow operator (->).
#include <stdio.h>struct Engine { int horsepower; int cylinders; };
int main() { struct Engine myEngine = {450, 8}; // Create a pointer to the structure struct Engine *ptr = &myEngine; // Accessing members using the arrow operator printf("Horsepower: %d\n", ptr->horsepower); printf("Cylinders: %d\n", ptr->cylinders); return 0; }
Which operator is used to access the members of a structure variable?