结构体笔记2
发布日期:2021-11-20 10:17:40
浏览次数:13
分类:技术文章
本文共 939 字,大约阅读时间需要 3 分钟。
结构体做函数参数
//将结构体作为参数向函数中传递 //传递方式有两种:1.值传递 2.参数传递#include "pch.h"#include#include using namespace std;struct student{ string name; int age; int score;};//打印学生信息函数//1.值传递void printstudent1(struct student s){ cout << "姓名" << s.name << "年龄" << s.age << "分数" << s.score << endl;}//2.指针传递void printstudent2(struct student* p)//定义一个指针存传过来的地址{ cout << "姓名" << p->name << "年龄" << p->age << "分数" << p->score << endl;}int main(){ //结构体变量 struct student s; s.name = "aa"; s.age = 18; s.score = 85; struct student* p; printstudent1(s); printstudent2(&s);//传入地址}
结构体中const的使用
作用:用const防止误操作
#include "pch.h"#include#include using namespace std;struct student{ string name; int age; int score;};void printstu(const struct student* s) { //s->age = 150; //如果指针传递可以修改实参的数值,为了避免误操作,进行const,只可以读,不可以修改。 cout << s->age;}int main(){ struct student s = { "aa",10,85 }; printstu(&s);}
转载地址:https://blog.csdn.net/weixin_43223362/article/details/102595699 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月17日 03时06分07秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Unity爆炸、闪电、火焰、雷雨特效Demo
2019-04-27
使用python登录和访问Confluence
2019-04-27
Unity2020中使用MemoryProfile卡死和报错的问题
2019-04-27
Unity中加载Texture2D不断消耗内存的问题
2019-04-27
手把手教你使用Unity制作一个飞机喷射火焰尾气的粒子效果
2019-04-27
gitee使用教程,创建项目仓库并上传代码
2019-04-27
Unity3D 上传日志
2019-04-27
Unity3D 序列帧
2019-04-27
Unity3D 动态创建图集并压入精灵(NGUI)
2019-04-27
C# 将Excel转xml
2019-04-27
Unity3D log写入文件
2019-04-27
Unity3D动态创建摄像机
2019-04-27
unity 根据纪元时间(1970/1/1)转换为DateTime
2019-04-27
C# 字符串md5加密
2019-04-27
unity3D 拖拽旋转3D物体
2019-04-27
unity3D LineRender的使用:插值移动终点
2019-04-27
unity3d 根据手指触摸的位置去放置UI
2019-04-27
加锁单例
2019-04-27
unity3D 让粒子在UI上播放
2019-04-27
C++的dll导出类
2019-04-27