Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 30th, 2012  |  syntax: None  |  size: 0.51 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Sending variable length arrays over a network?
  2. typedef struct network_packet
  3. {
  4.     char id;
  5.     int message_size;
  6.     int data[];
  7. } __attribute__((packed)) network_packet;
  8.        
  9. unsigned char* buffer;
  10. //...fill the buffer and make sure it's the right size -- endian is taken care of
  11. //via library
  12.  
  13. network_packet* packet_ptr = (network_packet*)buffer;
  14.  
  15. //access the 10th integer in the packet if the packet_ptr->message_size
  16. //is long enough
  17. if (packet_ptr->message_size >= 10)
  18.     int tenth_int = packet_ptr->data[9];