Structures are a way of storing many different values in variables of potentially different types under the same name. This makes it a more modular program, which is easier to modify because its design makes things more compact. Structs are generally useful whenever a lot of data needs to be grouped together--for instance, they can be used to hold records from a database or to store information about contacts in an address book. In the contacts example, a struct could be used that would hold all of the information about a single contact--name, address, phone number, and so forth.
Syntax:
struct Tag {
Members
};
Where Tag is the name of the entire type of structure and Members are the variables within the struct. To actually create a single structure the syntax is
struct Tag name_of_single_structure;
To access a variable of the structure it goes
name_of_single_structure.name_of_variable;
Example:
struct example {
int x;
};
struct example an_example;
an_example.x = 33;
Here is an example program:
struct database {
int id_number;
int age;
float salary;
};
int main()
{
database employee;
employee.age = 22;
employee.id_number = 1;
employee.salary = 12000.21;
}