C Structures
An introduction to C Structures
Using the struct keyword we can create complex data structures using basic C types.
A structure is a collection of values of different types. Arrays in C are limited to a type, so structures can prove to be very interesting in a lot of use cases.
This is the syntax of a structure:
struct <structname> {
//...variables
};
Example:
struct person {
int age;
char *name;
};
You can declare variables that have as type that structure by adding them after the closing curly bracket, before the semicolon, like this:
struct person {
int age;
char *name;
} flavio;
Or multiple ones, like this:
struct person {
int age;
char *name;
} flavio, people[20];
In this case I declare a single person variable named flavio, and an array of 20 person named people.
We can also declare variables later on, using this syntax:
struct person {
int age;
char *name;
};
struct person flavio;
We can initialize a structure at declaration time:
struct person {
int age;
char *name;
};
struct person flavio = { 37, "Flavio" };
and once we have a structure defined, we can access the values in it using a dot:
struct person {
int age;
char *name;
};
struct person flavio = { 37, "Flavio" };
printf("%s, age %u", flavio.name, flavio.age);
We can also change the values using the dot syntax:
struct person {
int age;
char *name;
};
struct person flavio = { 37, "Flavio" };
flavio.age = 38;
Structures are very useful because we can pass them around as function parameters, or return values, embedding various variables within them, and each variable has a label.
It’s important to note that structures are passed by copy, unless of course you pass a pointer to a struct, in which case it’s passed by reference.
Using typedef we can simplify the code when working with structures.
Let’s make an example:
typedef struct {
int age;
char *name;
} PERSON;
The structure we create using
typedefis usually, by convention, uppercase.
Now we can declare new PERSON variables like this:
PERSON flavio;
and we can initialize them at declaration in this way:
PERSON flavio = { 37, "Flavio" }; download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.