美文网首页
C++ 结构体

C++ 结构体

作者: 小潤澤 | 来源:发表于2020-03-16 19:54 被阅读0次

结构体简介

结构体是拥护自定义的数据类型,允许用户储存不同的数据类型

语法:struct 结构体名{结构体成员列表};

结构体创建变量的方式
1.struct结构体名 变量名
2.struct结构体名 变量名={成员1值,成员2值}
3.定义结构体时顺便创建变量

定义结构体

#include<iostream>
using namespace std;
#include<string>

struct Student
{
    //成员列表
   //姓名
    string name;
   //年龄
    int age;
   //分数
    int score;
};

int main(){
  //创建具体学生
  //方法一
  struct Student s1;
  //通过.来访问
  s1.name = "张三";
  s1.age = 18;
  s1.score =   100;

  cout<<"姓名:  "<<s1.name<<"年龄:  "<<s1.age<<"分数:  "<<s1.score<<end1;
  
  //方法二
  struct Student s2 = { "李四" ,19 , 80 };
  cout<<"姓名:  "<<s2.name<<"年龄:  "<<s2.age<<"分数:  "<<s2.score<<end1;

system("pause");
  return 0;
}

那么第三种穿件结构体的方法是

#include<iostream>
using namespace std;
#include<string>

struct Student
{
    //成员列表
   //姓名
    string name;
   //年龄
    int age;
   //分数
    int score;
}s3;

int main(){
  //创建具体学生
  //方法三
  s3.name = "王五";
  s3.age = 20;
  s3.score = 60;

  cout<<"姓名:  "<<s3.name<<"年龄:  "<<s3.age<<"分数:  "<<s3.score<<end1;
  
system("pause");
  return 0;
}

在定义结构体的时候顺便建立属性

结构体数组

结构体的数组的作用是将自定义结构体放到数组里面做存储

#include<iostream>
using namespace std;
#include<string>

//定义结构体
struct Student
{
    //成员列表
   //姓名
    string name;
   //年龄
    int age;
   //分数
    int score;
};

int main(){
  //创建结构体数组
  struct Student stuArray[3] = 
  {
      {"张三" ,18 , 100},
      {"李四" ,28 , 99},
      {"王五" , 38 , 66}
  };
    //赋值
    stuArray[2].name = "赵六";
    stuArray[2].age = 80;
    stuArray[2].score = 60;
  
  //遍历结构体数组 
    for (int i = 0; i < 3;i++)
   {
       cout<<"姓名:  "<<stuArray[i].name
           <<"年龄:  "<<stuArray[i].age
           <<"分数:  "<<stuArray[i].score<<end1;
   }
    
  system("pause");
  return 0;
}

结构体指针

通过指针访问结构体成员

#include<iostream>
using namespace std;
#include<string>

//定义结构体
struct Student
{
    //成员列表
   //姓名
    string name;
   //年龄
    int age;
   //分数
    int score;
};

int main(){
    struct Student s = {"张三" , 18, 100 };
    //通过指针只想结构体变量
    Student *p = &s;
   //通过指针访问结构体变量内的数据
    cout<<"姓名: "<<p->name<<"年龄: "<<p->age<<"分数: "<<p->score<<end1;

   system("pause");
   return 0;
}

结构体嵌套

即结构体成员是另一个结构体

#include<iostream>
using namespace std;
#include<string>

//定义结构体
struct student
{
    //成员列表
   //姓名
    string name;
   //年龄
    int age;
   //分数
    int score;
};

struct teacher
{
    //成员列表
   //姓名
    int id;
   //年龄
    string name;
   //分数
    int age;
   //辅导的学生
    struct student stu;
};

int main(){
   teacher t;
   t.id = 10000;
   t.name = "老王";
   t.age = 50;
   t.stu.name = "小王";
   t.stu.age = 20;
   t.stu.score = 60;
   
   cout<<"老师姓名: "<<t.name<<"老师编号: "<<t.id<,"老师年龄: "<<t.age<<"老师的辅导学生姓名: "<<t.stu.name<<"学生年龄: "<<t.stu.age<<"学生考试分数为: "<<t.stu.score;

system("pause");
   return 0;
}

结构体做函数参数

作用是结构体作为参数向函数传递

我们首先看下值传递

#include<iostream>
using namespace std;
#include<string>

struct student
{
    //成员列表
   //姓名
    string name;
   //年龄
    int age;
   //分数
    int score;
};

void printStudent1(struct student s)
 {
     cout<<"子函数1姓名:  "<<s.name<<"年龄:  "<<s.age<<"分数:  "<<s.score<<end1;
 }
int main(){
  //创建具体学生
  struct student s;
  s.name = "张三";
  s.age = 20;
  s.score = 85;

  printStudent1(s);
  
system("pause");
  return 0;
}

地址传递

#include<iostream>
using namespace std;
#include<string>

struct student
{
    //成员列表
   //姓名
    string name;
   //年龄
    int age;
   //分数
    int score;
};

void printStudent2(struct student *p)
 {
     cout<<"子函数2姓名:  "<<p->name<<"年龄:  "<<p->age<<"分数:  "<<p->score<<end1;
 }

int main(){
  //创建具体学生
  struct student s;
  s.name = "张三";
  s.age = 20;
  s.score = 85;

  printStudent2(&s);
  
system("pause");
  return 0;

我们可以通过void printStudent2(const student p)来防止误操作

相关文章

  • JSON数据转C++结构体

    JSON数据自动生成C++结构体 JSON数据自动生成C++结构体背景nlohmann/json基础Python自...

  • Texture之ASDisplayNodeInternal.h

    c++容器 c++类型:atomic_uint 罕见的c++语法 初始化结构体 c结构体 检查指定类是否重写了另一...

  • C++常用容器复习

    cin>>a cout<

  • c++类相关

    c++结构体与c结构体的区别 c语言 c++ c++类的使用 类的权限管理 对类的成员的访问做限制 private...

  • 01-OC对象的本质

    OC是通过C/C++的什么数据结构实现我们的OC对象呢 结构体--OC对象的本质就是C/C++的结构体 Class...

  • C++中结构体

    C++中结构体并不是C中的结构体了 C++的结构体更像是一种特殊的类 他与类一样 可以有public privat...

  • NDK开发—结构体、共用体与C++基础(三)

    目录 结构体、共用体与C++基础11、结构体字节对齐2、共用体3、C++ 输出函数符号兼容引用字符串C字符串字符串...

  • C++总结

    结构体成员变量是什么? C++结构体怎么定义 struct aa { }或者typedef struct aa {...

  • C++系列 --- 结构体、权限修饰符、类简介

    一、结构体 结构体:自定义的数据类型 C++ 中的结构和C中的结构有什么区别? C++中的结构除具备了C中的所有功...

  • JNI总结

    java调用c/c++ 在C中:JNIEnv 结构体指针别名env二级指针 在C++中:JNIEnv 是一个结构体...

网友评论

      本文标题:C++ 结构体

      本文链接:https://www.haomeiwen.com/subject/yhbqshtx.html