Mapping a byte array to a struct in C


/ Published in: C++
Save to your folder(s)



Copy this code and paste it in your HTML
  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);

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.