C语言当时为了解决面对对象的问题,提出了结构体进来暂时解决面向对象思维;
结构体使用
#include <iostream>
#include <string>
using namespace std;
struct stu{
char name[20];
int age;
bool is_man;
}student;
int main() {
student.age = 10;
cout<<"请输入名字:";
cin>>;
student.is_man = false;
cout<<student.is_man<<endl;
cout<<student.age<<endl;
cout<<<<endl;
return 0;
}
- 结构体指针
#include <iostream>
#include <string>
using namespace std;
struct stu{
char name[20];
int age;
bool is_man;
}student;
int main() {
// 结构体指针
stu *user;
user = &student;
student.age = 10;
cout<<"请输入名字:";
cin>>;
student.is_man = false;
cout<<student.is_man<<endl;
cout<<student.age<<endl;
cout<<<<endl;
cout<<user->is_man<<endl;
cout<<user->age<<endl;
cout<<user->name<<endl;
return 0;
}
- 结构体数组
```c++
#include <iostream>
#include <string>
using namespace std;
struct stu{
char name[20];
int age;
bool is_man;
}student;
int main() {
// 结构体指针
stu *user;
user = &student;
stu teachers[20] ;
student.age = 10;
cout<<"请输入名字:";
cin>>;
student.is_man = false;
cout<<student.is_man<<endl;
cout<<student.age<<endl;
cout<<<<endl;
cout<<user->is_man<<endl;
cout<<user->age<<endl;
cout<<user->name<<endl;
for (int i = 0; i < 20; ++i) {
strcpy_s(teachers[i].name,);
teachers[i].age = student.age;
teachers[i].is_man = student.is_man;
}
for (int i = 0; i < 20; ++i) {
cout<<teachers[i].age;
}
return 0;
}