C Structures
A structure groups several values that belong together, of different types, under one name. A student has a name, a roll number and marks; a point has an x and a y. With struct you create your own type for such data.
Declaring and using a structure
#include <stdio.h>
#include <string.h>
struct Student {
char name[20];
int roll;
double marks;
};
int main(void) {
struct Student s;
strcpy(s.name, "Asha");
s.roll = 1;
s.marks = 84.5;
printf("%s (roll %d): %.1f\n", s.name, s.roll, s.marks);
return 0;
}Asha (roll 1): 84.5
The parts of a structure are its members, reached with a dot: s.roll. A string member is filled with strcpy, because arrays cannot be assigned with =.
Initialising
A structure can get all its values at once, in the order of the members, or by member name:
#include <stdio.h>
struct Point {
int x;
int y;
};
int main(void) {
struct Point a = {3, 4};
struct Point b = {.y = 10, .x = 1};
struct Point c = a; // structures can be copied with =
c.x = 99;
printf("a = (%d, %d)\n", a.x, a.y);
printf("b = (%d, %d)\n", b.x, b.y);
printf("c = (%d, %d)\n", c.x, c.y);
return 0;
}a = (3, 4) b = (1, 10) c = (99, 4)
Unlike arrays, whole structures can be copied with =; c is an independent copy, so changing it does not change a.
Arrays of structures
#include <stdio.h>
struct Student {
char name[20];
int roll;
double marks;
};
int main(void) {
struct Student cls[3] = {
{"Asha", 1, 84.5},
{"Meera", 2, 91.5},
{"Ravi", 3, 77.0}
};
int best = 0;
for (int i = 0; i < 3; i++) {
printf("%-6s roll %d %.1f\n", cls[i].name, cls[i].roll, cls[i].marks);
if (cls[i].marks > cls[best].marks) best = i;
}
printf("Best: %s with %.1f\n", cls[best].name, cls[best].marks);
return 0;
}Asha roll 1 84.5 Meera roll 2 91.5 Ravi roll 3 77.0 Best: Meera with 91.5
Structures and functions
A structure passed to a function is copied, like any value. To change it, or to avoid copying a big structure, pass a pointer. With a pointer, members are reached with the arrow ->: p->marks means (*p).marks.
#include <stdio.h>
struct Account {
int number;
double balance;
};
void deposit(struct Account *acc, double amount) {
acc->balance += amount;
}
int main(void) {
struct Account a = {1001, 500.0};
deposit(&a, 250.0);
printf("Account %d: %.2f\n", a.number, a.balance);
return 0;
}Account 1001: 750.00
Why use structures?
Without structures, the data of three students needs three separate arrays: names, rolls and marks, which must always be kept in the same order. One array of structures keeps each student's data together, so it can be passed to a function, sorted or copied as one unit. Structures are also how C programs model real things: a date, a bank account, a point on the screen, a player in a game.
The size of a structure
sizeof(struct Student) is often a little more than the sizes of the members added up. The compiler inserts padding so that each member starts at an address that suits its type, which makes the program faster. Never assume the size; use sizeof.
Practice exercises
- Define
struct Bookwith a title, an author and a price; fill an array of three books and print the cheapest. - Write a function that takes a pointer to a book and raises its price by 10 percent.
- Write a function that returns the distance between two
struct Pointvalues.