【学习笔记】【C语言】结构体
1.定义结构体变量的3种方式
1> 先定义类型,再定义变量(分开定义)
struct Student
{
int age;
};
struct Student stu;
2> 定义类型的同时定义变量
struct Student
{
int age;
} stu;
struct Student stu2;
3> 定义类型的同时定义变量(省略了类型名称)
struct
{
int age;
} stu;
2.结构体类型的作用域
1> 定义在函数外面:全局有效(从定义类型的那行开始,一直到文件结尾)
2> 定义在函数(代码块)内部:局部有效(从定义类型的那行开始,一直到代码块结束)
3.代码
1>结构体
1 /* 2 数组:只能由多个相同类型的数据构成 3 4 结构体:可以由多个不同类型的数据构成 5 */ 6 #include <stdio.h> 7 8 int main() 9 { 10 //int ages[3] = {[2] = 10, 11, 27}; 11 12 13 //int ages[3] = {10, 11, 29}; 14 15 // 1.定义结构体类型 16 struct Person 17 { // 里面的3个变量,可以称为是结构体的成员或者属性 18 int age; // 年龄 19 double height; // 身高 20 char *name; // 姓名 21 }; 22 23 // 2.根据结构体类型,定义结构体变量 24 struct Person p = {20, 1.55, "jack"}; 25 p.age = 30; 26 p.name = "rose"; 27 28 printf("age=%d, name=%s, height=%f\n", p.age, p.name, p.height); 29 30 /* 错误写法 31 struct Person p2; 32 p2 = {30, 1.67, "jake"}; 33 */ 34 35 struct Person p2 = {.height = 1.78, .name="jim", .age=30}; 36 //p2.age = 25; 37 38 return 0; 39 }
2>结构体内存分析
1 #include <stdio.h> 2 int main() 3 { 4 5 6 return 0; 7 } 8 9 // 补齐算法 10 void test1() 11 { 12 struct Student 13 { 14 int age;// 4个字节 15 16 char a; 17 18 //char *name; // 8个字节 19 }; 20 21 struct Student stu; 22 //stu.age = 20; 23 //stu.name = "jack"; 24 // 补齐算法(对齐算法) 25 // 结构体所占用的存储空间 必须是 最大成员字节数的倍数 26 27 int s = sizeof(stu); 28 printf("%d\n", s); 29 } 30 31 // 结构体内存细节 32 void test() 33 { 34 // 1.定义结构体类型(并不会分配存储空间) 35 struct Date 36 { 37 int year; 38 int month; 39 int day; 40 }; 41 42 // 2.定义结构体变量(真正分配存储空间) 43 struct Date d1 = {2011, 4, 10}; 44 45 46 struct Date d2 = {2012, 8, 9}; 47 48 // 会将d1所有成员的值对应地赋值给d2的所有成员 49 d2 = d1; 50 d2.year = 2010; 51 52 printf("%d - %d - %d\n", d1.year, d1.month, d1.day); 53 54 printf("%d - %d - %d\n", d2.year, d2.month, d2.day); 55 /* 56 printf("%p - %p - %p\n", &d1.year, &d1.month, &d1.day); 57 58 int s = sizeof(d1); 59 printf("%d\n", s); 60 61 */ 62 }
3>注意点
1 #include <stdio.h> 2 // 从这行开始,一直到文件结尾,都是有效(跟全局变量一样) 3 struct Date 4 { 5 int year; 6 int month; 7 int day; 8 }; 9 10 int a; 11 12 void test2() 13 { 14 struct Date 15 { 16 int year; 17 }; 18 // 这里使用的是test2函数内部的struct Date类型 19 struct Date d1 = {2011}; 20 21 22 // 结构体类型也是有作用域,从定义类型的那一行开始,一直到代码块结束 23 struct Person 24 { 25 int age; 26 }; 27 28 struct Person p; 29 30 a = 10; 31 } 32 33 int main() 34 { 35 struct Date d1 = {2009, 8, 9}; 36 37 38 test2(); 39 40 // 不能使用test2函数中定义的类型 41 // struct Person p2; 42 43 return 0; 44 } 45 46 // 定义结构体变量 47 void test() 48 { 49 // 定义结构体变量的第3种方式 50 struct { 51 int age; 52 char *name; 53 } stu; 54 55 struct { 56 int age; 57 char *name; 58 } stu2; 59 60 61 /*结构体类型不能重复定义 62 struct Student 63 { 64 int age; 65 }; 66 67 struct Student 68 { 69 double height; 70 }; 71 72 struct Student stu; 73 */ 74 75 /* 错误写法:结构体类型重复定义 76 struct Student 77 { 78 int age; 79 double height; 80 char *name; 81 } stu; 82 83 struct Student 84 { 85 int age; 86 double height; 87 char *name; 88 } stu2;c 89 */ 90 91 /* 92 这句代码做了两件事情 93 1.定义结构体类型 94 2.利用新定义好的类型来定义结构体变量 95 */ 96 // 定义变量的第2种方式:定义类型的同时定义变量 97 /* 98 struct Student 99 { 100 int age; 101 double height; 102 char *name; 103 } stu; 104 105 struct Student stu2; 106 */ 107 108 /* 109 // 定义变量的第1种方式: 110 // 1.类型 111 struct Student 112 { 113 int age; 114 double height; 115 char *name; 116 }; 117 118 // 2.变量 119 struct Student stu = {20, 1.78, "jack"}; 120 */ 121 }
4>结构体数组
1 int main() 2 { 3 struct RankRecord 4 { 5 int no; // 序号 4 6 int score; // 积分 4 7 char *name; // 名称 8 8 }; 9 /* 10 struct RankRecord r1 = {1, "jack", 5000}; 11 struct RankRecord r2 = {2, "jim", 500}; 12 struct RankRecord r3 = {3, "jake",300}; 13 */ 14 15 //int ages[3] = {10, 19, 29}; 16 17 //int ages[3]; 18 // 对齐算法 19 // 能存放3个结构体变量,每个结构体变量占16个字节 20 // 72 21 /* 22 int no; // 序号 4 23 char *name; // 名称 8 24 int score; // 积分 4 25 */ 26 // 48 27 /* 28 int no; // 序号 4 29 int score; // 积分 4 30 char *name; // 名称 8 31 */ 32 struct RankRecord records[3] = 33 { 34 {1, "jack", 5000}, 35 36 {2, "jim", 500}, 37 38 {3, "jake",300} 39 }; 40 41 records[0].no = 4; 42 // 错误写法 43 //records[0] = {4, "rose", 9000}; 44 45 for (int i = 0; i<3; i++) 46 { 47 printf("%d\t%s\t%d\n", records[i].no, records[i].name, records[i].score); 48 } 49 50 //printf("%d\n", sizeof(records)); 51 52 53 return 0; 54 }
转载于:https://www.cnblogs.com/dssf/p/4604847.html
【学习笔记】【C语言】结构体相关推荐
- 学习笔记17-C语言-结构、联合、枚举
结构: struct 结构是由程序猿自己设计的一种数据类型,用于描述一个事务的各项数据,由若干个不同的基础数据类型组成.设计结构:struct 结构体名{类型 成员名:...}:定义结构体变量stru ...
- 如何在结构体里面套结构体_Rust 学习笔记-13 Rust 结构体
什么是结构体 数组用于表示值的同构集合.类似地,结构体是Rust中另一个用户定义的数据类型,它允许我们组合不同类型的数据项,包括另一个结构体.结构体将数据定义为键值对. 定义一个结构体 struct ...
- Swift学习笔记 (十八) 结构体和类
结构体和类作为一种通用而又灵活的结构,成为了人们构建代码的基础.你可以使用定义常量.变量和函数的语法,为你的结构 体和类定义属性.添加方法. 与其他编程语⾔所不同的是,Swift 并不要求你为自定 ...
- # 遍历结构体_C#学习笔记05--枚举/结构体
一.枚举 当变量的取值范围是固定的几个, 例如性别--男,女; 英雄类型 -- 法师, 刺客.战士, 射手等等. 这时就可以使用枚举类型, 会更加简洁方便. 1.1.定义: 访问修饰符 enum 枚举 ...
- C#学习笔记_12_枚举结构体
12_枚举&结构体 枚举 是一种数据类型 适用于某些取值范围有限的数据 语法: [访问权限修饰符] enum 枚举名 { 枚举值 } 枚举名遵循大驼峰命名法 枚举一般情况下是和switch c ...
- linux中c语言结构体详解,Linux C语言结构体-学习笔记
Linux C语言结构体简介 前面学习了c语言的基本语法特性,本节进行更深入的学习. 预处理程序. 编译指令: 预处理, 宏定义, 建立自己的数据类型:结构体,联合体,动态数据结构 c语言表达式工具 ...
- python展开 c函数中的宏预处理_Linux C语言结构体-学习笔记
Linux C语言结构体简介 前面学习了c语言的基本语法特性,本节进行更深入的学习. 预处理程序. 编译指令: 预处理, 宏定义, 建立自己的数据类型:结构体,联合体,动态数据结构 c语言表达式工具 ...
- C 语言结构体引用,引用 C 语言结构体学习
引用 C 语言结构体学习 这篇文章很基础,是个学习的好资料,所以收藏了: 1.直接声明结构体变量: struct{ int length; int width; }box1; 这样就声明了一个名为bo ...
- c语言2个字符串可以相互赋值吗,c语言结构体2之变量赋值于字符串
#include #include struct dangdang { char email[]; char name[]; char addr[]; int num; int bugnum; cha ...
- C语言结构体和结构体指针的简单用法
C语言结构体和结构体指针的简单用法 这里总结一下自己的学习笔记,关于C语言当中的结构体指针的用法,以及结构体简单使用. 简单介绍一下今天出场的嘉宾–>结构体和结构体指针 什么是结构体: 1,定义 ...
最新文章
- 网吧无盘服务器从30台----115台的经验(二)
- 复旦大学跑步爱好者协会章程(终稿)
- 为mongodb加上权限
- 提取验证码到winform上webbroswer和axwebbroswer
- vim捐赠_#PayItBackwards-一位freeCodeCamp毕业生如何向事业捐赠10,000美元
- oracle ogg00423,【案例】Oracle报错PLS-00378 PLS-00439产生原因和MOS官方解决办法
- 用函数判断考试得分:
- 根据周次显示日期范围_Elasticsearch根据日期价格范围搜索酒店且排序
- 隐马尔科夫-维特比算法
- 在linux中查看服务,linux中怎么查看服务状态
- Fitting Bayesian structural time series with the bsts R package
- 简历javaweb项目描述怎么写_从事java开发工作简历怎样写工作描述
- 一次简单的PC游戏汉化
- HRBUST1151-魔女
- 天津大学计算机学院博士生导师,天津大学2020年博士研究生导师名单
- C#网络TCP客户端的实现
- Python nonlocal
- PS CC 2018 切片复制问题解决方法
- python无法运行图像_OpenCV Python不使用imread()打开图像
- case计算机英语,CASE
热门文章
- 2017二级c语言成绩,2017全国计机等级考试二级C语言知识点超全整(打印版).docx
- MAVEN [ERROR] 不再支持源选项 5。请使用 7 或更高版本。
- 片偏移字段的值怎么算_搞懂钢丝网片计算原理,怎么算都不怕出错!
- classcastexception异常_内部类、异常以及 LeetCode 每日一题
- ROS2 on android,ROS2 通过Debian安装ROS2
- android 创建文件夹_Android安全(四)数据库 之 SQLite数据库
- 这样理解PWM,想不懂都难!
- datax 导入数据中文乱码_浅谈数据同步之道
- LL-verilog语法多位宽全加器
- pandas 如何把时间转成index_pandas将字段中的字符类型转化为时间类型,并设置为索引...