当前位置:网站首页>结构体和联合体
结构体和联合体
2022-07-22 10:40:00 【LH2HA3】
一.结构体
1.为什么要提出结构体
我们知道数组是用来存储一组相同数据类型的,那么如果现在要存储一些不同类型的数据该用什么来实现呢?
此时结构体就有了用武之地,它可以同时处理多种数据类型数据。(结构体是一种数据类型,程序员可自己设计)
2.定义
关键字是:struct
用实例来说明其定义:
struct ebil{
int age;
char name[20];
};//此处分号是强制的
struct ebil e; 这里的e是类型为结构体ebil的变量。(当申明结构体变量时系统才会为结构体分配内存) 初始化:
struct ebil ec={20,"李明"};
struct ebil ed={21,"王鑫"};
说明:结构体声明在main函数之前是对所有函数可见的,但声明在main函数中只在main中可见。 3.访问结构体成员
结构体变量申请完毕后,如何使用变量的数据成员才是我们最关心的问题。访问结构体变量的数据
成员使用点号来引用——点操作符或者结构体成员操作符。
e.age;
e.name;
4.结构体指针
申明结构体指针: struct ebil *pv;
使用结构体指针来访问结构体数据成员变量使用箭头操作符:
pv->age;
pv->name;
5.结构体数组当我们需要许多相同类型结构体时就可以使用结构体数组了。
struct ebil er[50];
同样我们还可以使用结构体指针数组: struct ebil *p[50];
当想要动态为指针数组分配内存是可以使用: p[i]=(struct ebil *)malloc(sizeof(struct ebil));
6.结构体作为函数参数结构体作为一种数据类型当然可以作为函数的参数进行传递,只需要将结构体变量传递给函数即可:
#include <iostream>
#include<stdio.h>
#include<conio.h>
struct str{
int age;
char name[20];
};
void function(struct str p);
int main(int argc, char** argv) {
struct st t={20,"李明"};//初始化
function(t);
return 0;
}
void function(struct str p){
printf("结构体第一个成员变量age=%d",p.age);
printf("结构体第二个成员变量name=%s",p.name);
}
既然结构体可以作为函数的参数,那么也可以作为函数的返回值——函数可以返回一个结构体指针。
在返回结构体指针时,需要将结构体实例的地址保存在一个指针中。比如:函数function返回一个结构体指针,pv是指针
那么可以:pv=function();然后通过pv来获得结构体中的数据成员。
7.结构体的拷贝结构体的拷贝只仅限于类型相同的结构体变量,其拷贝的方法有两种方法。
第一种:数据成员依次拷贝
struct ebil b1,b2;
b1.age=b2.age;
b1.name=b2.name;
第二种:结构体变量直接拷贝(简单) struct ebil e1,e2;
e1=e2;
八.嵌套结构体结构体中允许嵌套结构体:比如对一个人来说,姓名和年龄可以使用嵌套结构:
struct birthday{
int year;
int month;
int day;
};
struct body{
char name[20];
struct birthday b;
};
二.联合体(关键字 union)和结构体类似,但是联合体中的所有数据成员共用一块最大内存块,而及结构体所占内存是所有数据成员的所占
内存之和。
边栏推荐
- C language (Itoa function)
- c语言bitField
- 初次见面 多多关照
- Aminer paper recommendation
- Process fork
- Latex在VSCODE中编译报错`Recipe terminated with error. Retry building the project.
- 【文献阅读与想法笔记13】Pre-Trained Image Processing Transformer
- Latex compiles and reports errors in vscode `recipe terminated with error Retry building the project.
- 【FPGA】 SPI协议
- 【面试:基础篇04:插入排序】
猜你喜欢
Latex在VSCODE中编译报错`Recipe terminated with error. Retry building the project.
Binary search (recursive function)
考虑器件匹配和寄生最小化的共质心电容器布局生成
使用多种加权方法在 SAR ADC 中放置二进制加权电容阵列
Vimplus modifies the terminal font to droid Sans Mono nerd font
Mise en œuvre de la pile de chaînes (langage c)
Pre training weekly 39: deep model, prompt learning
基于非线性最坏情况分析的电荷缩放 DAC 中电容器的新棋盘放置和尺寸调整方法
vimplus修改终端字体为Droid Sans Mono Nerd Font
DEFORMABLE DETR 论文精度,并解析网络模型结构
随机推荐
[literature reading and thought notes 13] pre trained image processing transformer
【面试:基础篇05:快速排序】
1038 Recover the Smallest Number (30 分)
栈实现(C语言)
Pat-2021 winter exam (Full Score)
【FPGA】:ip核--XADC
MySQL index
DETR 論文精讀,並解析模型結構
1049 Counting Ones (30 分)
1053 Path of Equal Weight (30 分)
JSON output to file line by line in format
Binary search (recursive function)
配置属于自己的Vim-编辑器之神
7-2 Rank a Linked List (25 分)
[FPGA]: IP core --ddr3
Chain stack implementation (C language)
【FPGA】:ip核--ibert
New attribute of class (elementary understanding)
信号耦合约束下扭曲共质心电容器阵列的布线能力
1072 gas station (30 points)