标签类目:memcpy

将一个结构体拷贝到字符数组缓冲区,再读出

分类:C/C++

  1. #include <stdio.h> 
  2. #include <memory.h> 
  3.  
  4. typedef struct 
  5. { 
  6. int cmd;
  7. void *param;
  8. }st;
  9.  
  10. char buf[50];
  11.  
  12. int main(int argc, char* argv[]) 
  13. { 
  14. st st1,*st2;
  15. st1.cmd = 1;
  16. st1.param = &st1.cmd;
  17.  
  18. memcpy(buf, &st1, sizeof(st));
  19.  
  20. st2 = (st*)buf;
  21. printf("cmd:%d\n", st2->cmd);
  22. printf("param:%d\n", st2->param);
  23. return 0;
  24. }

返回顶部