c++ 结构体-struct
结构体,类似于数据结构的实现体,其数据访问控制权限是public
C中格式
c中结构体不可以包含函数
//两种方法等价
//定义
typedef struct Student
{
int age;
string name;
} S;
struct Student
{
int age;
string name;
}
typedef struct Student S;
int main() {
struct Student me; // 或者 S me;
return 0;
}
C++中格式
注意:在C++中,结构体可以包含函数,当定义了与结构体同名的函数Student,此时Student只是代表函数
struct Student
{
int age;
string name;
} ;
struct Student
{
int age;
string name;
} stu;
typedef struct Student
{
int age;
string name;
} stu1;
//stu是结构体变量,stu1是结构体类型
//stu.age
//stu1 s1; s1.age;
本文介绍了C和C++中结构体的使用,包括它们的区别和如何声明及访问结构体成员。在C中,结构体不能包含函数,而在C++中则可以。示例展示了如何定义和初始化结构体变量,并强调了`struct`关键字在不同语言环境下的应用。此外,还提到了结构体类型的别名创建和成员访问方式。
1208

被折叠的 条评论
为什么被折叠?



