What Is a Structure?
In this lesson, you will learn to keep values that belong together in one place. So far, each variable has held one value, and an array has held several values of the same type. But think of a product in a shop: it has a name (text), a price (a decimal number) and a count, which says how many pieces you are buying (a whole number). With what you know so far, you would need three variables:
char bread_name[20] = "Bread";
double bread_price = 32.5;
int bread_count = 2;For one product, that works. For ten products, you would need thirty variables, and only their names would tell you which of them belong together. A function that prints a product would need three parameters. An array doesn't help either: all its elements must have the same type, and a name, a price and a count don't.
A structure solves this. It groups several values, even of different types, into one whole. First you describe what the structure contains:
struct product {
char name[20];
double price;
int count;
};This is the definition of a structure. The keyword struct says that a structure follows, and product is its name. Inside the curly braces are its members: the values that every product has. Each member is written like a variable declaration, with a type and a name, but without a value. Write the definition above main, as you do with functions, so that the compiler knows it before you use it.
The definition itself creates no variable and holds no values. It is like an empty form with the fields Name, Price and Count: it only says what will be filled in. It gives you a new type, struct product, and you can create variables of that type, just as you create variables of type int:
struct product bread = {"Bread", 32.5, 2};The values in the curly braces fill in the members in the order in which the definition lists them: "Bread" goes into name, 32.5 into price and 2 into count. It looks like initializing an array, but the values can have different types.
To get to one member, write the name of the variable, a dot and the name of the member: bread.price is the price of the bread. A member is used like an ordinary variable of its type. bread.name is a string, so you print it with %s; bread.price is a double, so %.2f:
#include <stdio.h>
struct product {
char name[20];
double price;
int count;
};
int main(void) {
struct product bread = {"Bread", 32.5, 2};
printf("%s: %.2f, %d pieces\n", bread.name, bread.price, bread.count);
return 0;
}This prints:
Bread: 32.50, 2 piecesprintf can't print a whole structure at once, just as it can't print a whole array. Print its members one by one.
The most common mistake is the semicolon after the closing curly brace. A function doesn't have one there, but a structure must. If you leave it out in the program above, the compiler reports:
main.c:7:2: error: expected ';' after struct
7 | }
| ^
| ;
1 error generated.The first line says where the problem is: on line 7, column 2, right after the }. The ^ points at that spot, and the ; below it shows what is missing.
Instructions
Above main, define a structure struct pet for a pet, with three members: name (an array of 20 characters, char), age (a whole number, int) and weight (a decimal number, double). Don't forget the semicolon after }.
In main, in place of the comment, create a variable rex of type struct pet with the name "Rex", the age 3 and the weight 12.5.
Below it, print one line about Rex: Rex is 3 years old and weighs 12.5 kg. Read all three values from the members of rex, and print the weight with %.1f.
Start programming for free
1/9