Posted By

brentroady on 09/27/06


Tagged

struct


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

copyleft


Mapping a byte array to a struct in C


 / Published in: C++
 

URL: http://geekswithblogs.net/taylorrich/archive/2006/08/21/88665.aspx

  1. #pragma pack(1)
  2. typedef struct
  3. {
  4. int id;
  5. char[50] text;
  6. } MESSAGE;
  7.  
  8. // Send a message
  9. MESSAGE msg;
  10. msg.id = 1;
  11. strcpy(msg.text, "This is a test");
  12. send(socket, (char*)&msg);
  13.  
  14. // Receive a message
  15. char buffer[100];
  16. recv(socket, buffer, 100);
  17. MESSAGE* msg = (MESSAGE*)buffer;
  18. printf("id=%d\n", msg->id);
  19. printf("text=%s\n", msg->text);

Report this snippet  

You need to login to post a comment.