当前位置:网站首页>一个结构体 = 另一个结构体(同类型结构体之间可直接赋值操作)
一个结构体 = 另一个结构体(同类型结构体之间可直接赋值操作)
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;
}
这里要提一句,数组不能像结构体那样直接赋值。
边栏推荐
- Week 7 Review
- @ApiModel 和 @ApiModelProperty
- String comparison size in MySQL (date string comparison problem)
- [Mianjing] Mihayou data development on one side and two sides
- redis进行持久化时,有新的写操作将如何解决——写时复制
- ImportError: libGL.so.1: cannot open shared object file: No such file or directory
- 赶紧进来!!!教你用C语言写三子棋小游戏
- day11--shell脚本
- MySQL分组后排序
- MySQL中JOIN的用法
猜你喜欢

Cloud server installation and deployment of Nacos 2.0.4 version

磷脂-聚乙二醇-酰肼,DSPE-PEG-Hydrazide,DSPE-PEG-HZ,MW:5000

二维数组实战项目--------《三子棋》

Redis simple study notes

MySql创建数据表

subprocess.CalledProcessError: Command 'pip install 'thop'' returned non-zero exit status 1.

@Accessors 注解详解

青蛙跳台阶:我如何得知它是一道斐波那契数列题?——应用题破题“三板斧”

MySQL分组后取最大一条数据【最优解】

最新,每天填坑,Jeston TX1 精卫填坑,第一步:刷机
随机推荐
np.unique() function
API 低代码开发:接口大师,一套开发、管理和提供接口的产品框架
排序学习笔记(二)堆排序
PowerManagerService灭屏超时流程
docker 安装 sqlserver中的坑点
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
云服务器安装部署Nacos2.0.4版本
基本运算符
MySQL中JOIN的用法
@Accessors 注解详解
Error: with open(txt_path,'r') as f: FileNotFoundError: [Errno 2] No such file or directory:
subprocess.CalledProcessError: Command ‘pip install ‘thop‘‘ returned non-zero exit status 1.
JVM学习----垃圾回收--G1
RHCSA第二天
Good Key, Bad Key (thinking, temporary exchange, classic method)
The difference between the knowledge question and answer session with the knowledge
subprocess.CalledProcessError: Command 'pip install 'thop'' returned non-zero exit status 1.
科研试剂DMPE-PEG-Mal 二肉豆蔻酰磷脂酰乙醇胺-聚乙二醇-马来酰亚胺
Chapter 10 Clustering
ImportError: libGL.so.1: cannot open shared object file: No such file or directory