Suppose we want to store information about users using a specific service. Such information can include the username, email, address, service mode, and such. To store such information, we can go about creating each attribute as a standalone variable. However, when we have ten plus users, the code can spiral out of control and become very difficult and tiresome to read.
To solve this, we can create a structure. Inside the structure, we can store all the attributes shared by all the users and then add unique variables for each user.
Let us take a look at various examples to see how to implement this.
How to Define a Structure in C
To define a structure in C, we use the struct keyword followed by the structure’s name. After the name, we have a pair of curly braces where we add the members.
Consider the syntax below:
{
/* data */
type member_name;
type member_name2;
type member_name3;
...
type member_nameN;
};
The structure’s name can be anything you want as long as it adheres to the naming convention of the C programming language.
We can implement an example structure of the user analogy as:
{
char username[20];
char email[225];
char address[50];
int age;
bool registered;
};
How to Create Structure Variables
There are two main ways to create structure variables. The first one is to declare them like normal variables, and the other is to set them using curly braces.
The example below shows how to declare structure variables as standard C variables.
{
char username[20];
char email[225];
char address[50];
int age;
bool registered;
};
int main(int argc, char const *argv[])
{
struct users user1, user2, user3
return 0;
}
The other method of creating structure variables is as shown below:
{
char username[20];
char email[225];
char address[50];
int age;
bool registered;
} user1, user2, user3;
In this example, we create them during structure declaration.
Structure Member Init
You cannot initialize the structure members during creation as there is no memory allocated for the type.
To initialize the members of a structure, you use the curly braces as shown below:
{
char username[20];
char email[225];
char address[50];
int age;
bool registered;
};
int main(int argc, char const *argv[])
{
struct users user1 = {"myusername", "zero@user.name", 35, true}
return 0;
}
Access Structure Members
To access the members of a structure, we use the dot operator, starting with the structure name, a dot, and the name of the member.
{
char username[20];
char email[225];
char address[50];
int age;
bool registered;
};
int main(int argc, char const *argv[])
{
struct users user1 = {"myusername", "zero@user.name", 35, true}
user1.email = "zero@vuln.io"
return 0;
}
In this example, we access the members of user1.
Typedef Keyword
We use the typedef keyword to create an alias for the data types, making the code more readable.
For example:
{
char username[20];
char email[225];
char address[50];
int age;
bool registered;
} u;
int main(int argc, char const *argv[])
{
u user1 = {"myusername", "zero@user.name", 35, true}
user1.registered = false
return 0;
}
In the example above, we created an alias “u” for the “users” structure. Therefore, we do not need to call struct users every time. We can use “u” as defined above.
Structure Pointers
You can also have a pointer to a structure. Doing this allows you to access the members using the -> operator.
For example:
{
char username[20];
char email[225];
char address[50];
int age;
bool registered;
} u;
int main(int argc, char const *argv[])
{
u user1 = {"myusername", "zero@user.name", 35, true}
// pointer to another structure
u *user_ptr = &user1
user_ptr -> username = "ptrusername"
return 0;
}
Conclusion
This guide covers the basics of working with structures in the C programming language.
Thank you for reading!
from https://ift.tt/3zly5QK

0 Comments