当前位置:网站首页>一个结构体 = 另一个结构体(同类型结构体之间可直接赋值操作)
一个结构体 = 另一个结构体(同类型结构体之间可直接赋值操作)
2022-08-02 03:22:00 【a只如初见】
两个同类型结构体变量可以直接赋值,不同类型结构体不能直接赋值。
#include <stdio.h>
struct TEST{
int a;
int b;
char c[3];
char *ptr;
};
int main(){
struct TEST a={
1,2,{
'a','b','c'}, (char *)"jkjk"};
struct TEST b={
0};
printf("a.a=%d a.b=%d a.c[0]=%c a.ptr=%s\n", a.a, a.b, a.c[0], a.ptr);
printf("b.a=%d b.b=%d b.c[0]=%c\n", b.a, b.b, b.c[0]);
b = a;
printf("b.a=%d b.b=%d b.c[0]=%c b.ptr=%s\n", b.a, b.b, b.c[0], b.ptr);
return 0;
}
运行结果(VS2019):
还有种方法使两个同类型结构体内容相同就是用memcpy函数来拷贝,也是可以的,因为结构体本身就是一个内存区。
int main() {
struct TEST a = {
1,2,{
'a','b','c'}, (char *)"jkjk" };
struct TEST b = {
0 };
printf("a.a=%d a.b=%d a.c[0]=%c a.ptr=%s\n", a.a, a.b, a.c[0], a.ptr);
printf("b.a=%d b.b=%d b.c[0]=%c\n", b.a, b.b, b.c[0]);
//b = a;
memcpy((char *)&b, (char *)&a, sizeof(a));
printf("b.a=%d b.b=%d b.c[0]=%c b.ptr=%s\n", b.a, b.b, b.c[0], b.ptr);
return 0;
}
这里要提一句,数组不能像结构体那样直接赋值。
边栏推荐
- cross-domain problem solving
- nucleo stm32 h743 FREERTOS CUBE MX配置小记录
- 3 minutes to take you to understand WeChat applet development
- sh: 1: curl: not found
- 活体检测 Adaptive Normalized Representation Learning for GeneralizableFace Anti-Spoofing 阅读笔记
- 周日数据库作业
- 分布式领域最重要的一篇论文,到底讲了什么?
- mysql中exists的用法详解
- COCO数据集训练TPH-YoloV5
- MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细
猜你喜欢
MySQL分库分表
ThunderBirde无法登录问题、pycharm调试一直收集数据、RuntimeError: CUDA error: device-side assert triggered等疑难杂症解决
Redis笔记进阶篇:万字长文-整理Redis,各种知识点,建议收藏
Cloud server installation and deployment of Nacos 2.0.4 version
PCL—point cloud data segmentation
Redis简单学习笔记
mysql8.0安装教程与配置(最详细)操作简单
Redis的集群模式
HCIP-第十天-BGP综合实验
[Remote Control Development Basic Tutorial 3] Crazy Shell Open Source Formation UAV-ADC (Joystick Control)
随机推荐
化学试剂磷脂-聚乙二醇-羟基,DSPE-PEG-OH,DSPE-PEG-Hydroxyl,MW:5000
HCIP-第十一天-MPLS+BGP
UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the index ing argu
Knowledge Engineering Assignment 2: Introduction to Knowledge Engineering Related Fields
网站与服务器维护怎么做?
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
MySQL常见的索引
UserWarning:火炬。meshgrid:在以后的版本中,它将被要求通过索引ing argu
通过PS 2021 将网页图标抠下来
mysql中exists的用法详解
API 低代码开发:接口大师,一套开发、管理和提供接口的产品框架
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
@Autowired详解[email protected]在static属性上的使用
[Mianjing] Mihayou data development on one side and two sides
@DateTimeFormat注解
Debian 10 NTP 服务配置
Chemical reagent Phospholipid-polyethylene glycol-hydroxyl, DSPE-PEG-OH, DSPE-PEG-Hydroxyl, MW: 5000
删库后!除了跑路还能干什么?
@Autowired注解的使用
动态代理工具类