π§±C Structures
What are C structures
Introduction
Structures are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure.
To make it clearer, I will use the example of a library that wants to store information about books :
Book title
Book author
Book ID
Difference between array and structures
Arrays allow you to store multiple variables of the same data type, an array of int, an array of char, etc.
Structures allow you to store multiple variables of different data types. You could have one int member, one char member, multiple double members, etc.
Create a Structure
Structures are generally declared in a header file so it is usable everywhere the header file is included.
They are created using the struct keyword like this :
struct [structure tag]
{
member definition;
member definition;
...
member definition;
} [one or more structure variables];Using structure tag and variables
Structure tag and structure variables are concepts that are easily explained with examples.
I'll explain the difference between the two usage.
a and c will have the same content, as expected.
b and d will also have the same content, as expected.
The difference lies in the fact that you can create new variables based on the struct s_st1 whenever you want in your code, but won't be able to do so for the anonymous structure.
If you want to create a new variable based on the structure you'll have to do the following :
As you can see, using a named structure can be easier if you need to declare a new variable later in your code.
For example, our s_book structure would be declared like this :
Accessing structure members
To access a member of a structure, we have to use the member access operator ..
To use it, we have to put a . between the structure variable name and the member we want to access. See the example below to find out how it's used in a program.
When the above code is compiled and executed it produces the following result :
Accesing structure pointer members
To access a member of a structure pointer, we have to use another member access operator ->.
To use it, we have to put a -> between the structure variable name and the member we want to access. See the example below to find out how it's used in a program.
When the above code is compiled and executed it produces the following result :
Defining a type for your structure (typedef)
We use the typedef keyword to create an alias name for data types. It is commonly used with structures to simplify the syntax of declaring variables.
Look at the following example :
We can use typedef to write an equivalent with a simplified syntax :
Naming convention
To respect the 42 Norm, we have to name our structures and typedef in a certain way.
This also makes it a lot easier to follow.
Structure names must start with s_ and typedefs must start with t_.
Why structs ?
Suppose you want to store information about, to keep the same example, a book : title, author, id. You can create different variables title, author, id to store this information.
What if you need to store information of more that one book ? Now, you need to create different variables for each informaiton per book : title2, author2, id2, etc.
A better approach would be to have a collection of all related information under a single named s_book structure and use it for every book.
Sources
Last updated
Was this helpful?
