What is a Structure?
A structure in C is like a container that can hold different types of data together. Think of it as a custom data type that groups related information under one name. It's similar to a form where you fill in different fields - each field can be a different data type!
For example, if you want to store information about a student, you might need:
- Name (string)
- Age (integer)
- Grade (float)
Instead of creating separate variables for each piece of information, you can group them together using a structure.
Defining a Structure
To create a structure, you use the struct keyword followed by a name and the member variables inside curly braces:
struct Student {
char name[50];
int age;
float grade;
};
This creates a blueprint for a Student structure. Think of it as creating a template - you haven't created an actual student yet, just defined what a student should contain.
Declaring Structure Variables
Once you've defined a structure, you can create variables of that structure type:
struct Student student1;
struct Student student2;
You can also declare variables while defining the structure:
struct Student {
char name[50];
int age;
float grade;
} student1, student2;
Accessing Structure Members
To access the members of a structure, you use the dot operator (.):
#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int age;
float grade;
};
int main() {
struct Student student1;
// Assigning values
strcpy(student1.name, "Alice");
student1.age = 20;
student1.grade = 85.5;
// Accessing and printing values
printf("Name: %s\n", student1.name);
printf("Age: %d\n", student1.age);
printf("Grade: %.1f\n", student1.grade);
return 0;
}
Initializing Structures
You can initialize a structure when you declare it:
struct Student student1 = {"Bob", 19, 92.3};
Or you can use designated initializers (C99 and later):
struct Student student1 = {
.name = "Charlie",
.age = 21,
.grade = 88.7
};
Complete Example
Here's a complete example that demonstrates structure basics:
#include <stdio.h>
#include <string.h>
struct Book {
char title[100];
char author[50];
int pages;
float price;
};
int main() {
// Declare and initialize a structure variable
struct Book book1 = {
.title = "The C Programming Language",
.author = "Dennis Ritchie",
.pages = 272,
.price = 29.99
};
// Declare another structure variable
struct Book book2;
// Assign values to book2
strcpy(book2.title, "Learn C the Hard Way");
strcpy(book2.author, "Zed Shaw");
book2.pages = 298;
book2.price = 34.95;
// Display information
printf("Book 1:\n");
printf("Title: %s\n", book1.title);
printf("Author: %s\n", book1.author);
printf("Pages: %d\n", book1.pages);
printf("Price: $%.2f\n\n", book1.price);
printf("Book 2:\n");
printf("Title: %s\n", book2.title);
printf("Author: %s\n", book2.author);
printf("Pages: %d\n", book2.pages);
printf("Price: $%.2f\n", book2.price);
return 0;
}
Key Points to Remember
- Structures group related data of different types
- Use
structkeyword to define a structure - Use the dot operator (
.) to access structure members - Structure members can be of any data type
- You can initialize structures during declaration or assign values later
- Structures help organize code and make it more readable
Structures are fundamental building blocks in C programming and are especially useful when dealing with complex data that has multiple attributes!