Structures in C
A structure is an object in which there can be several other objects.
Different types.
Consider a website as such an object.
Let's name the structure Website
| The site may have an address or name | Website name |
| Year of creation | Website year |
| Administrator | Website admin |
| Rating or quality index | Website rating |
| And a lot of other objects. | |
There can be thousands of sites and all this information needs to be stored somehow.
You can create an array, but what type ? url is most likely char, year int, admin char, rating double
Doing several arrays is very expensive.
To solve this problem, structures will suit. It is enough to declare the type of structure, for example Website,
List what objects we want to store and what types they have. Then we can
Create new objects such as structure to declare that they are Site type and access their nested objects.
We will create a website structure and several objects such as Website, in other words – websites.
Website is the name of a structure, something like a type. Analogue is int, char, float. The only difference is that you create this type yourself.
Everyone will have a name, year and rating.
After they are created, you can refer to objects by appending their name to the structure name with a dot.
For example: Object name.
#include <stdio.h>
#include <string.h>
struct Website {
char name[80];
int year;
float rating;
}
// So far, no objects have been created – we have just defined a structure named Website.
// This structure contains three members (fields) of different types.
site0, site1 = {"AndreyOlegovich.ru", 2010, 160.1}
// 1. Only now that we have declared two variables site0 and site1 type Website.
// We have objects of this structure.
// Site1 members are initialized.
// site0 so far "empty"
;
int main()
{
printf("site1 -- Name: %s year: %d rating: %.1f\n", site1.name, site1.year, site1.rating);
// 2. Standard method of assigning values to structure objects
struct Website site2 = {"HeiHei.ru", 2018, 30.0};
printf("site2 -- Name: %s year: %d rating: %.1f\n", site2.name, site2.year, site2.rating);
// 3. Assign values to objects separately
struct Website site3;
// Working with int and float is simple.
site3.year = 2010;
site3.rating = 160.1;
// But char needs some extra work.
strcpy(site3.name, "TopBicycle.ru");
printf("site3 -- Name: %s year: %d rating: %.1f\n", site3.name, site3.year, site3.rating);
// 4. Fill in the structure from the keyboard using the function scanf();
struct Website site4;
printf("Enter the name, year and rating for site4\n");
scanf("%s %d %f", site4.name, &site4.year, &site4.rating);
printf("site4 -- Name: %s year: %d rating: %.1f\n", site4.name, site4.year, site4.rating);
// 5. Copying the structure
site0 = site4;
printf("site0 -- Name: %s year: %d rating: %.1f\n", site0.name, site0.year, site0.rating);
return 0;
}
The function scanf() You can read it. here
gcc -o struc struct.c
./struct
site1 -- Name: AndreyOlegovich.ru year: 2010 rating: 160.1 site2 -- Name: HeiHei.ru year: 2018 rating: 30.0 site3 -- Name: TopBicycle.ru year: 2010 rating: 160.1 Enter the name, year and rating for site4 aredel.com 2018 10 site4 -- Name: aredel.com year: 2018 rating: 10.0 site0 -- Name: aredel.com year: 2018 rating: 10.0
Article author: Andrei Olegovich