当前位置:网站首页>C语言结构体
C语言结构体
2022-06-30 16:40:00 【*山河万里*】
一、结构体
C语言提供了基本的数据结构,例如 char 、short 、int 、float....等类型;这些偶称为内置类型。怎样设计出来属于自己的类型?
比如:当我们定义一个人的时候,人的不同属性就比较难用同一个数据类型来定义了,因为人的身高、年龄、体重等属性往往需要不同数据类型,在这个时候,我们便引入结构体这个概念
使用结构体来封装一些属性,设计出新的类型,在C语言中称为结构体类型。结构是一些值的集合,这些值称为成员变量。结构的每个成员可以是不同类型的变量
结构声明(structure declaration)
struct 结构体名
{
成员列表(可以是基本的数据类型,指针,数组或其他结构类型)
};
/*struct关键词表示接下来是一个结构。*/
struct People
{
char s_id[8];
char s_name[8];
char s_sex[4];
int s_age;
};
注意;
- 关键字struct是数据类型说明符,指出下面说的是结构体类型;
- 标识符Student是结构体的类型名;
- 最后的分号一定要写;
二、结构体的定义及初始化
结构体的声明只是进行一个简单的描述,实际上在没有定义结构体类型变量之前,它是不会在内存中分配空间的。
int main()
{
struct People people;//局部变量--放在栈区
return 0;
}
* struct 结构体名称 结构体变量名
结构体是一种数据类型,也就是说可以用它来定义变量。就像一个“模板”,定义出来的变量都具有相同的性质。可以将结构体比作“图纸”,结构体变量比作“零件”,根据同一张图纸生产出来的零件的特性都是一样的;
- 结构体是一种数据类型,是创建变量的模板,不占用内存空间;
- 结构体变量才包含了实实在在的数据、需要存储空间;
三、结构体成员访问
- 结构体变量使用 . 访问;
- 结构体变量.对象
#include<stdio.h>
#include<string.h>
struct Date
{
int year;
int month;
int day;
};
struct Student
{
char s_name[20];
struct Date birthday;
float score;
};
int main()
{
struct Student stu = { "liuwen",2000,10,1,99.9 };
printf("name=%s\nbirtyday=%d.%d.%d\nscore=%f\n", stu.s_name, stu.birthday.year, stu.birthday.month, stu.birthday.day, stu.score);
stu.score = 77;
printf("name=%s\nbirtyday=%d.%d.%d\nscore=%f\n", stu.s_name, stu.birthday.year, stu.birthday.month, stu.birthday.day, stu.score);
return 0;
注意:对结构体变量整体赋值有三种情况
- 定义结构体变量(用{}初始化)
- 用已定义的结构体变量初始化
- 结构体类型相同的变量可以作为整体相互赋值;
在C语言中不存在结构体类型的强制转换。
四、结构体变量和指针
内置类型可以定义指针变量,结构体类型也可以定义结构体类型指针;
结构体类型指针访问成员的获取和赋值形式:
(1)(*p). 成员名(.的优先级高于*,(*p)两边括号不能少)
(2) p->成员名(->指向符)
#include<stdio.h>
#include<string.h>
struct Inventory//商品
{
char description[20];//货物名
int quantity;//库存数据
};
int main()
{
struct Inventory sta = { "iphone",20 };
struct Inventory* stp = &sta;
char name[20] = { 0 };
int num = 0;
(*stp).quantity = 30;
stp->quantity = 30;
strcpy_s(name,sizeof(stp->description),stp->description);
printf("%s %d\n", stp->description, stp->quantity);
printf("%s %d\n", (*stp).description, (*stp).quantity);
return 0;
}
结构体和函数
#include<stdio.h>
#include<string.h>
#define _CRT_SECURE_NO_WARNINGS
struct School
{
char s_name[20];//学校
int s_age;
};
void Print_a(struct School sx)
{
printf("%s %d\n", sx.s_name, sx.s_age);
}
void Print_c(struct School* sp)
{
printf("%s %d\n", sp->s_name, sp->s_age);
}
int main()
{
struct School sx = { "xi'an",100 };
Print_a(sx);
Print_c(&sx);
return 0;
}
五、struct和class的区别
1、什么是class?
class(类)是面向对象编程的基本概念,是一种自定义数据结构类型,通常包含字段、属性、方法、属性、构造函数、索引器、操作符等。因为是基本的概念,所以不必在此详细描述,读者可以查询相关概念了解。我们重点强调的是.NET中,所有的类都最终继承自System.Object类,因此是一种引用类型,也就是说,new一个类的实例时,对象保存了该实例实际数据的引用地址,而对象的值保存在托管堆(managed heap)中。
2、什么是struct?
struct(结构)是一种值类型,用于将一组相关的信息变量组织为一个单一的变量实体。所有的结构都继承自System.ValueType类,因此是一种值类型,也就是说,struct实例分配在线程的堆栈(stack)上,它本身存储了值,而不包含指向该值的指针。所以在使用struct时,我们可以将其当作int、char这样的基本类型类对待。
- 1.默认继承权限,class的继承按照private继承处理,struct的继承按照public继承处理;
- 2.成员的默认访问权限。class的成员默认是private权限,struct默认是public权限。
- 3.struct是值类型,class 是对象类型
六、参考
边栏推荐
- 【剑指Offer】剑指 Offer 53 - II. 0~n-1中缺失的数字
- Redis (VI) - master-slave replication
- Canvas cloud shape animation
- NFT: 开启加密艺术时代的无限可能
- Redis (I) - data type
- Compile and generate busybox file system
- MIT科技评论2022年35岁以下创新者名单发布,含AlphaFold作者等
- 5g has been in business for three years. Where will innovation go in the future?
- Map集合
- 基于SSH的客户关系CRM管理系统
猜你喜欢
How to write a technical proposal
Send the injured baby for emergency medical treatment. Didi's driver ran five red lights in a row
Deep understanding of JVM (III) - memory structure (III)
MIT science and Technology Review released the list of innovators under the age of 35 in 2022, including alphafold authors, etc
Exploration and practice of "flow batch integration" in JD
NFT: 开启加密艺术时代的无限可能
Login box tricks
K-line diagram must be read for quick start
每日面试1题-如何防止CDN防护被绕过
6 张图带你搞懂 TCP 为什么是三次握手?
随机推荐
Ten thousand volumes - list sorting [01]
Simulation of campus network design based on ENSP
Post penetration file system + uploading and downloading files
【架构】1366- 如何画出一张优秀的架构图
MySQL之零碎知识点
AnimeSR:可学习的降质算子与新的真实世界动漫VSR数据集
Development: how to install offline MySQL in Linux system?
Exploration and practice of "flow batch integration" in JD
MIT science and Technology Review released the list of innovators under the age of 35 in 2022, including alphafold authors, etc
Redis (VIII) - enterprise level solution (I)
leetcode:787. The cheapest transfer flight in station K [k-step shortest path + DFS memory + defaultdict (dict)]
基于eNSP的校园网设计的仿真模拟
Add code block in word (Reprint)
送受伤婴儿紧急就医,滴滴司机连闯五个红灯
墨天轮沙龙 | 清华乔嘉林:Apache IoTDB,源于清华,建设开源生态之路
Exch:Exchange Server 2013 即将终止支持
What will be the game changes brought about by the meta universe?
零基础也能做Apple大片!这款免费工具帮你渲染、做特效、丝滑展示
如何写一个技术方案
Redis (IV) - delete policy