完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
|
相关推荐
1个回答
|
|
串口传输 结构体 就是 把结构体 转换为 数组 ,然后传输 数组中的 char。
即 struct - - - - - - ->> char [ ] ; 然后 char[ ] - - - -- - - - -->> struct . 给出一下简单实例代码 : #include #include /*--------------------------start file---------------------------------*/ typedef struct { int id; char name[10]; int score; }student; int struct_array(char* des, student *src) { int num; char *temp = (char *) src ; for(num=0;num return num; } void array_struct(student * des ,char * src) { char* temp = (char *) des; int num; for(num=0 ;num < sizeof(student);num++) *(temp++)= src[num] ; } void show_student(student * name) { printf("id is : %dn",name->id); printf("name is : %sn",name->name); printf("score is : %d n",name->score); } void show_array(char *str ,int len ) { printf("len is : %d n" ,len); while(len--) { putchar('|'); putchar(*str++); } printf("|n"); } int main() { char name[10] ="laomao"; student laomao = { 123 , {"laomao"} , 99 } ; char save[100] ; int len ; student * answer; len = struct_array(save, &laomao) ; show_array(save , len); array_struct(answer , save) ; show_student(answer); show_student(&laomao); return 0; } /*---------------------------end file----------------------------------*/ 运行结果: ------------------------------------------------------- len is : 20 |{| | | |l|a|o|m|a|o| | | | | | |c| | | | id is : 123 name is : laomao score is : 99 id is : 123 name is : laomao score is : 99 --------------------------------------------------------------- |
|
|
|
只有小组成员才能发言,加入小组>>
2611 浏览 0 评论
806浏览 1评论
249浏览 1评论
578浏览 0评论
312浏览 0评论
528浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-2-2 22:10 , Processed in 1.299952 second(s), Total 79, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号