当前位置:网站首页>C语言:结构体简单语法总结
C语言:结构体简单语法总结
2022-07-29 04:12:00 【白的夜gxw】
我们在编程中要表达的信息通常不只是一个数字或者是一个字符。程序可能处理具有多种属性的实体。例如通过姓名、地址、电话号码、和其他信息表示一名客户。C结构可以把这些信息放在一个单元内。这便是结构体。
建立结构声明
结构声明描述了一个结构的组织布局。声明类似下面这样:
struct book{ char title[20]; char author[20]; float value; }dick;这样,该函数便创建了一个结构变量dick,该变量的结构布局是book.值得一提的是,结构的标记名是可选的。
定义结构变量
struct book library;编译器执行这行代码便创建了一个结构变量library.编译器使用book模板为该变量分配空间;
在结构变量的声明中,struct book 所起的作用相当于一般声明中的int或者float.
结构体变量的定义有俩种形式:
形式1.
struct book{ char title[20]; char author[20]; float value; }dick;形式2.
struct book dick;初始化结构
结构变量同样也可以进行初始化:如下图所示
struct book library = { "the is a book", "money need 20", 1.95 };结构的初始化器
C99和C11为结构提供了指定初始化器,可以指定选择初始化结构成员中的某一项。
struct book surper = { .value=10.99};可以按照任意顺序使用指定初始化器
struct book gift = { .value = 10.99, .aulouth = "GXW", .riri = "ko" }声明和初始化结构指针
struct guy * him;和数组不同的是,结构变量名并不是结构变量的地址,因此要在结构变量名的前面加上一个&运算符。
him = &barny顺带一提,在有些系统中,一个结构的大小可能大于他各成员的大小之和。这是因为系统对数据进行校准的过程产生了一些缝隙。例如,有些系统必须把每个成员放在偶数地址上,或4的倍数的地址上。在这种系统中,结构的内部就存在没使用的缝隙。
向函数参数传递结构的信息:(以下三种)
struct book{
char name[20];
char author[20];
}BOOK;
1.传递结构成员
将结构的成员作为参数,直接传递给函数;
void MAX(BOOK.name);
2.传递结构的地址
将结构的地址作为参数,直接传递给函数;
void MAX(&BOOK);
3.传递结构
将结构作为参数,直接传递给函数
void MAX(BOOK);
其他结构特性
1.结构可以整体赋值
o_data=n_data;2.初始化相同的结构
struct names right = { "nihao",12}; struct names capit = right;//把一个结构初始化为另一个结构结构和指针的区别
指针作为参数的优点:执行快,只需要传递给一个地址
指针的缺点:无法保护数据,被调函数中的某些操作可能会意外的影响原来结构的操作。不过可以使用const
结构作为参数的优点:函数处理的是原始数据的副本,这保护了原始数据。代码风格也更加的清楚。
结构的缺点:传递结构浪费时间和存储空间尤其是把大型结构传递给函数,而只使用结构中的一俩个成员特别浪费。
复合字面量和结构
C99的复合字面量特性可用于结构和数组。如果只需要一个临时结构值。复合字面量很好用。例如:
value=(struct book) { "the idiot","Fyodor Dostoyev",6.99};struct book{ char MAX[20]; char MIN[20]; double a; }; int main() { app=(struct book) { "the idiot","Fyodor Dostoyev",6.99}; bpp=(struct book) { "idiot","Fyodor",8.65}; printf("%f",app.a); printf("%f",bpp.a); }把结构的内容保存到文件中
如何把结构的信息存储到文件中去呢?
方法1;fprintf()函数
缺点:不可以一次将一整块数据存储
方法2:fwrite()函数和fread()函数
fwrite(&prime,sizeof(struct book),1,pbook);
定位到prime结构变量开始的位置,并把结构中的所有字节都拷贝到与pbook相关的文件中。sizeof(struct book)告诉函数待拷贝的一块数据的大小,1表明一次拷贝一块数据。
边栏推荐
- [Openstack] keystone,nova
- [原理] 横向渗透的几种方式
- 伏英娜:元宇宙就是新一代互联网!
- The structure pointer must be initialized, and the pointer must also be initialized
- The output comparison function of Tim is introduced in detail through PWM breathing lamp and PWM controlled DC motor
- Array as function parameter -- pointer constant / constant pointer
- Safari's compatibility with Z-index
- C language: getchar () and cache
- Problems encountered in vscode connection SSH
- Locally call tensorboard and Jupiter notebook on the server (using mobaxterm)
猜你喜欢
![[paper translation] vectornet: encoding HD maps and agent dynamics from vectorized representation](/img/4b/150689d5e4809ae66a4297915ecd0c.png)
[paper translation] vectornet: encoding HD maps and agent dynamics from vectorized representation

C language to achieve three chess game (detailed explanation)

初识C语言(3)

基于STM32和阿里云的环境检测系统设计

RMAN do not mark expired backups

VScode连接ssh遇到的问题

Is the browser multi process or single process?
![[kvm] create virtual machine from kickstart file](/img/0e/292ccb6862e29d948ad6ece86b7945.png)
[kvm] create virtual machine from kickstart file

Three tier architecture of enterprise network

UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0x90 in position 614: ordinal not in range(128)
随机推荐
Copy products with one click from Taobao, tmall, 1688, wechat, jd.com, Suning, taote and other platforms to pinduoduo platform (batch upload baby details Interface tutorial)
淘宝商品详情接口(商品详情页面数据接口)
C语言实现三子棋游戏(详解)
索引的最左前缀原理
LCA board
Communication between parent-child components and parent-child components provide and inject
What the hell is this error? It doesn't affect the execution result, but it always reports errors when executing SQL... Connecting maxcomputer uses
Deep understanding of browser caching mechanism (HTTP)
STM32F103ZET6程序移植为C8T6+C8T6下载程序flash timeout的解决方案
Function pointer and callback function
[untitled]
伏英娜:元宇宙就是新一代互联网!
Locally call tensorboard and Jupiter notebook on the server (using mobaxterm)
MySQL第三篇
信号处理中的反傅里叶变换(IFFT)原理
Simple cases of inner connection and left connection
[kvm] install KVM
Asp.net MVC中文件夹中的控制器如何跳转到根目录的控制器中?
“蔚来杯“2022牛客暑期多校训练营2 H
After I get the winfrom specific control ID from the database, I need to find the corresponding control through this ID and assign a value to the text text of the control. What should I do