C Structures
A structure in C is a custom data type that groups related variables under one name. Each value inside a structure is called a member, and the members can have different types.
Structures are useful when several pieces of data belong together, such as a student’s name and score, or a point’s x and y coordinates.
Define A Structure Type
Use the struct keyword to define the shape of a structure. The definition lists the members that every variable of that structure type will contain.
#include <stdio.h>
struct Student {
char name[20];
int age;
double grade;
};
int main(void)
{
struct Student student = {"Maya", 19, 92.5};
printf("Name: %s\n", student.name);
printf("Age: %d\n", student.age);
printf("Grade: %.1f\n", student.grade);
return 0;
}
Output:
Name: Maya
Age: 19
Grade: 92.5
struct Student is the type name. The variable student contains three members: name, age, and grade. Use the dot operator, such as student.age, to access a member of a structure variable.
Assign Members After Declaration
You can initialize a structure when you declare it, as shown above, or assign to its members later. Arrays inside structures are still arrays, so use a string function such as strcpy to copy text into a character array member.
#include <stdio.h>
#include <string.h>
struct Book {
char title[40];
int pages;
};
int main(void)
{
struct Book book;
strcpy(book.title, "C Basics");
book.pages = 180;
printf("%s has %d pages.\n", book.title, book.pages);
return 0;
}
Output:
C Basics has 180 pages.
The statement book.pages = 180; assigns an integer member directly. The statement strcpy(book.title, "C Basics"); copies characters into the title array, because arrays cannot be assigned with = after declaration.
Arrays Of Structures
Structures become especially helpful when you need a list of records. An array of structures stores several complete structure values, one after another.
#include <stdio.h>
struct Point {
int x;
int y;
};
int main(void)
{
struct Point points[3] = {
{2, 4},
{5, 7},
{8, 1}
};
int i;
for (i = 0; i < 3; i++) {
printf("Point %d: (%d, %d)\n", i + 1, points[i].x, points[i].y);
}
return 0;
}
Output:
Point 1: (2, 4)
Point 2: (5, 7)
Point 3: (8, 1)
Each element of points is a full struct Point. First choose the array element with points[i], then use dot syntax to read one of its members.
Pass Structures To Functions
A structure can be passed to a function like other values. When passed this way, the function receives a copy of the structure. That is fine for small structures when the function only needs to read the data.
#include <stdio.h>
struct Rectangle {
int width;
int height;
};
int area(struct Rectangle rect)
{
return rect.width * rect.height;
}
int main(void)
{
struct Rectangle box = {6, 4};
printf("Area: %d\n", area(box));
return 0;
}
Output:
Area: 24
The function parameter rect has type struct Rectangle, so the function can use rect.width and rect.height. Later, when you combine structures with pointers, you can pass a structure’s address to let a function modify the original value efficiently.
Important Structure Rules
- Define the structure type before declaring variables of that type.
- Use
struct Name variable;to declare a structure variable. - Use the dot operator,
., to access members of a structure variable. - Members can have different types, including arrays and other structures.
- A structure groups data, but it does not automatically add behavior or functions.
The key idea is that structures let you model one thing with several related values. Next, you can build larger programs by combining structures with arrays, functions, and pointers.
