A structure in C is a data type intended for placing values of different types in one object. Useful when you need to combine several variables with different types under the same name. They make the program more compact and more convenient to manage. The structure has similar features with arrays and classes.
Arrays
Before talking about the structure in C, you need to describe an array.
There are arrays of one-dimensional, two-dimensional, three-dimensional. One-dimensional - this is one that has only one row with filled values. Two-dimensional - a one-dimensional array, inside of which there are other one-dimensional arrays.
An ordinary array in C is written like this: int a [4] = {1, 2, 3, 4}.
We see that a is the name, int is the data type, inside curly brackets {} there are values, between the square brackets [] the length is indicated, that is, the number of elements. The number of elements is static, equal to 4. This means that if the user adds a fifth value in this example, the compiler will throw an error. If the quantity is not initially known, they can be added later, but no value is given in square brackets.
Two-dimensional is declared in a similar way. For example, an array that contains 5 array elements, each containing 3 elements, is declared like this: int a [5] [3]. By analogy with one-dimensional, you cannot add anything so as not to get a compilation error.
Distinguish between dynamic and static. Static is one that holds a fixed amount of data, that is, has a constant length. Dynamic is understood to be one whose size is not limited; it can change during program execution. The dynamic array is initialized without specifying the exact quantity.
Classes
Class and structure are similar to each other, but differ in some nuances. What it is? This is an abstraction describing the methods of an object that does not yet exist. After creating an object, or, as it is called in another way, an instance of a class has specific properties. Methods can be used internally, externally, or for inheritance.
The class is declared like this:
class / * class name * /
{
private:
/ * private access specifier means that method control is possible only inside the class * /
public:
/ * makes properties available to other parts of the code * /
protected:
/ * inherited classes get the opportunity to use these properties * /
}.
What is a structure in C language
Designed to store several types of data. For example, to create a log directory, you need to have a list with the following parameters:
- publication date;
- issue number;
- title;
- cost.
Arrays could be used to solve this problem.
We declare an array with dates int date [20], numbers int number [20], char title [80], value int price [20].
Referring to the index, we get the required information. The output of information on the work under number 3 looks like this: cout << “release date:” date [3] “, number:” number [3] “, name:” title [3] “, cost:“ price [3]) .
The structure simplifies the recording, described as follows:
struct book {
int date;
int number;
char title [20];
int price [20];
}.
We see one of the main advantages - there are different types of variables. The programmer does not just save time - he simplifies the code, in the future it will be much easier for him to work.
Ad
Structures in C play a very important role - combining data of various types.
First you need to specify the name of the structure and property.
struct name
{
type member;
}
Struct is the keyword, it starts the declaration, name is the name, type is the data type, member is the name of the element.
It is declared like this:
name name2, where name is the name specified when creating the structure, and name2 is the name of the variable.
Variables can be declared at the creation stage.
struct name
{
type member;
} name2;
The first and second examples are equivalent to each other.
If there is a need to declare several variables, they are listed with a comma.
struct name
{
type member;
} name2, name3, name4.
Initialization
After the declaration, the structure in C needs to be initialized.
struct name
{;
char member
};
name name2;
name2.member = ”a”;
Initiation can occur at creation.
struct name
{
char member = “a”;
} name2;
The structure has the same syntax as the class. They have almost the same behavior, capabilities. Everything that is in the body of the class is by default inaccessible for use by other objects.
The structure is the other way around - all fields and methods are public. You can manually set the private access modifier and thus open access to other functions or classes.
Array of C structures
Arrays are many components of the same type. They are located next to each other, each of them is accessed by a numerical index. There are one-dimensional arrays, two-dimensional, three-dimensional.
One-dimensional only has one line and ne has the number of elements. The ad looks like this:
int a [16];
An array of structures in C is declared like this:
struct MyStruct
{
int a;
};
MyStruct obj1 [10];
In this example, we created MyStruct with an integer element named "a". We declare the variable obj1 [] - it is an array, consists of 10 elements.
When declaring several arrays of the same type, MyStruct obj1 [10], obj2 [5] is used, initialization occurs during the declaration. Looks like that:
struct MyStruct
{
int a;
} obj1 [10];
Creating an array of structures with dynamic memory allocation looks exactly like creating a simple dynamic array. A pointer to the C structure is used for this.
A pointer is a variable that does not contain a value, but points to that variable that has some value. Accordingly, the pointer contains the address of this variable to which it refers. For example, ptr = & var1 means that a variable with an ampersand sign is assigned only the address of the variable, but not the value itself. Now all var1 values are accessible via the ptr pointer variable.
Operation * refers to the contents of the cell pointed to by the variable after this symbol. For example, * ptr indicates that it contains values taken from a cell with an address to ptr.
To allocate memory for dynamic variables, use the new operation.
We have
struct MyStruct
{
string a;
}
We select a piece of memory, put there a certain value MyStruct * point = new MyStruct [1];
To delete dynamic variables, use the delete operation. To free up space, enter delete p [];
Access
All elements are public by default, so other classes can use them. To set or change some values, you first need to access the element and only then perform the appropriate actions.
Create myStruct with the variable name b.
struct myStruct {
string fio;
long num;
} b;
Turn to fio:
b.fio
and set an arbitrary value. For example, b.fio = “Ivanov”.
Consider this example.
struct myStruct {
string fio;
long num;
} tel [2] = {
{"Ivanov", 456756},
{"Petrov", 632345}
};
In this example, we have an array of structures with strings and numbers. To display the surname Ivanov, we use the following:
cout << myStruct tel [1] .fio;
When we want to get the value 456756, we execute cout << myStruct tel [1] .num.
Structure and function
It can be used as an argument to a function in a structure in C.
struct myStruct {
char text [100];
int value;
};
We have a variable value, a string of text for 100 characters. Create a menu variable of type myStruct: myStruct menu. In the following example, a function takes a pointer to a structure as an argument, and in the body of an unnamed function, these variables are initialized.
void item (myStruct menu)
{
sprintf (menu.text, "One item");
menu.value = 50;
}.
Conclusion
A structure is such a set, like an array, but all the elements can be of different types. It is very similar to a class, but differs in that the default properties are available for use by other classes, that is, they have a public specifier.
Created using the struct keyword, and properties are specified inside curly braces {}.
struct name
{
int member;
};
The announcement occurs at the creation stage or after.
struct name
{
int member;
} a;
or
struct name
{
int member;
} a;
struct name a.