我这样赋值,输出的时候会报错....为什么呢

结构体是这样的
struct bf{
unsigned char header;
unsigned char length;
unsigned char address;
unsigned char command;
unsigned char x[3];
unsigned char y[3];
unsigned char z[3];
unsigned char checksum;  

};
将下面这串字符串赋值取出一部分 赋值给上面的结构体
unsigned char a[] = {0x68,0x0D,0x00,0x04,0x20,0x10,0x10,0x58,0x00,0x04,0x05,0x00,0x00,0x32,0x68,0x0D,0x00,0x84,0x20,0x10,0x10,0x58,0x00,0x04,0x05,0x00,0x00,0x32,0x68,0x0D};

struct bf *buff;
buff = (struct bf *)malloc(sizeof(struct bf));
memcpy(buff,getbuf,14);

月关宝盒
浏览 107回答 2
2回答

收到一只叮咚

结构体赋值要求是内存对齐的。请问您这个字符串对齐了么?

缥缈止盈

常用的方法是:直接强制类型转换。前提是:1、接收的数据是先低后高的,2、结构体按1字节对齐#program pack(push) #program pack(1)结构体定义。。。#program pack(pop)struct bf * buff = NULL;buff = (bf*)&a[0];//呵,当然a也可以
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

MySQL
Java