Structure (struct) is an important data type in C language, which consists of a group of members of different types. Structs can be used to represent a complex data structure, such as information about a student, an employee record, or the dimensions of a rectangle.
After the structure is defined, structure variables can be declared, and each structure variable contains all members of the structure type. Each member in the structure variable can be of different data types, such as int, float, char, etc.
In a structure, each member has its own name and data type, which describes the memory layout of the structure. You can access structure members using the dot operator (.) or the arrow operator (->).
Structures can also be nested, that is, one structure can contain members of another structure type. This nesting can be used to represent more complex data structures.
Article directory
1. Definition and use of structures
In C++, a structure is a user-defined data type that can contain different types of data members. Structs are defined and used similarly to structs in C, but with some additional functionality and features in C++.
Sample code:
#include <iostream>
// 定义一个结构体
struct Person {
std::string name;
int age;
std::string address;
};
int main() {
// 创建一个结构体变量并初始化
Person person1 = {
"Alice", 25, "123 Main St."};
// 访问结构体的成员
std::cout << "Name: " << person1.name << std::endl;
std::cout << "Age: " << person1.age << std::endl;
std::cout << "Address: " << person1.address << std::endl;
// 修改结构体成员的值
person1.age = 30;
std::cout << "Updated Age: " << person1.age << std::endl;
return 0;
}
Code explanation:
In the above example, we first struct
defined a Person
structure named struct using keywords, and defined three data members in it: name
, , age
and address
. Next, in main()
the function, we created a struct variable of type named person1
and Person
initialized its members using the initializer list. Then, we use .
operators to access the members of the struct and output their values. Finally, we modified the value person1
of age
the member and output its new value again.
Second, the structure array
A structure array is a data structure that contains multiple structures. It can declare and initialize arrays by defining a structure type and using that type. Arrays of structures are often used to store data items with the same structure.
Sample code:
#include <stdio.h>
// 定义一个结构体类型
struct Person {
char name[50];
int age;
};
int main() {
// 声明并初始化一个结构体数组
struct Person people[3] = {
{
"Alice", 25},
{
"Bob", 30},
{
"Charlie", 35}
};
// 访问结构体数组的成员
for (int i = 0; i < 3; i++) {
printf("Person %d: %s, %d\n", i+1, people[i].name, people[i].age);
}
return 0;
}
Code explanation:
In the above code, we first define a Person
structure type called , which contains a character array name
and an integer age
. Then, in main()
the function, we declare and initialize an Person
array of three structures people
. Next, we for
iterate through the array of structs using a loop and use .
operators to access the members of each struct. Finally, we output the member values of each struct.
Three, the structure pointer
A pointer to a structure is a pointer type used to store the address of a structure variable. Through the structure pointer, we can indirectly access and modify the members of the structure.
Sample code:
#include <stdio.h>
// 定义一个结构体类型
struct Point {
int x;
int y;
};
int main() {
// 声明一个结构体变量
struct Point p1 = {
10, 20 };
// 声明一个指向结构体的指针
struct Point *p2 = &p1;
// 通过指针访问结构体的成员
printf("p2->x: %d\n", p2->x);
printf("p2->y: %d\n", p2->y);
// 通过指针修改结构体的成员
p2->x = 30;
p2->y = 40;
printf("p1->x: %d\n", p1.x);
printf("p1->y: %d\n", p1.y);
return 0;
}
Code explanation:
In the code above, we first define a Point
struct type called , which contains two integer members x
and y
. Then, in main()
the function, we declare a Point
variable of type struct p1
and initialize its members. Next, we declare a pointer to Point
the type p2
and p1
assign the address to it.
Through pointers p2
, we can use ->
operators to access members of the structure. For example, p2->x
accessing members p1
of a structure variable x
. At the same time, we can also modify the members of the structure through pointers, for example, p2->x
modify it to 30. Since p2
it points to p1
the address of , p2->x
modifications to will also affect p1.x
the value of . Finally, we output the modified p1.x
sum p1.y
value.
Fourth, structure nesting
Structure nesting refers to nesting another structure in a structure, which is usually used to build complex data structures. In C language, this can be achieved by defining nested structures.
Sample code:
#include <stdio.h>
// 定义一个结构体类型
struct Person {
char name[50];
int age;
};
// 定义一个嵌套的结构体类型
struct Student {
struct Person person; // 嵌套一个Person结构体
int grade;
};
int main() {
// 声明并初始化一个Student结构体变量
struct Student student = {
{
"Alice", 20},
90
};
// 访问Student结构体的成员
printf("Name: %s\n", student.person.name);
printf("Age: %d\n", student.person.age);
printf("Grade: %d\n", student.grade);
return 0;
}
Code explanation:
In the code above, we first define a Person
struct type called , which contains an array of chars name
and an integer age
. We then define a Student
nested struct type named , which contains a Person
member variable of the type person
and an integer grade
. In main()
the function, we declare and initialize a Student
variable of type struct student
. By using the dot operator, we can access student
members such as student.person.name
, , student.person.age
and student.grade
. The output will show Alice
, 20
and 90
.
5. Use structures as function parameters
The structure can be used as a function parameter to pass the value of the structure variable so that the structure variable can be operated within the function. This allows the data to be passed as a whole without the hassle of passing multiple separate variables.
Sample code:
#include <stdio.h>
// 定义一个结构体类型
struct Point {
int x;
int y;
};
// 定义一个函数,接受一个Point类型的结构体作为参数
void printPoint(struct Point p) {
printf("x: %d, y: %d\n", p.x, p.y);
}
int main() {
// 声明并初始化一个Point类型的结构体变量
struct Point p = {
10, 20 };
// 调用函数,将结构体变量作为参数传递
printPoint(p);
return 0;
}
Code explanation:
In the code above, we first define a Point
struct type called , which contains two integer members x
and y
. Then, we define a printPoint
function named , which takes a Point
struct of type as an argument. In main()
the function, we declare and initialize a Point
variable of type struct p
and pass it as a parameter to printPoint
the function. Inside the function, we can use p
the members of the structure variable to output the result.
6. The use of const in structures
In a structure, the const keyword can be used to modify member variables or member functions to ensure that their values or behaviors in the structure remain unchanged.
- const member variable:
struct MyStruct {
const int myConstVar;
};
In the above code, myConstVar
it is a const member variable, which is initialized when the structure is created, and its value cannot be modified. The initialization of const member variables should be done in the initialization list of the structure declaration.
- const member function:
struct MyStruct {
int myVar;
void myConstFunc() const {
// 这个成员函数不能修改myVar的值
}
};
In the above code, myConstFunc()
it is a const member function, which cannot modify the value of any member variable of the structure. This can be achieved by adding the const keyword at the end of the function declaration.
- const pointer member:
struct MyStruct {
int myVar;
const int* myConstPtr;
};
In the above code, myConstPtr
is a const pointer member, which points to a const integer. The value pointed to by this pointer cannot be modified. If you need to modify the value pointed to, you need to use a non-const pointer or pass the modified value to the structure through other methods.