-2

I have a problem to get databytes into my struct. I am programming in C. My received bytes looks like that:

Byte1 | Byte 2 | Byte 3| lengthData (2 Bytes) | data (variable)

My structure looks like that:

struct Packet {
   unsigned char byte1[1];
   unsigned char byte2[1];
   unsigned char byte3[1];
   unsigned char length[2];
   unsigned char * data; 
}*Packet

Via the read command I have the data in replay.

char * replay;
replay = (char*) malloc (MAX_DATA_LENGTH);
memset(replay, 0x00, MAX_DATA_LENGTH);
read(fd, replay, MAX_DATA_LENGTH)

Now I want to get my data bytes into the structure. Firstly I have to allocate memory for the pointer data. My question, how can I get the data with no big effort in the struct?

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
florian2840
  • 49
  • 3
  • 10
  • 2
    `unsigned char btyte1[1]` makes no sense just `unsigned char byte1`, and in the case `unsigned char length[2]` it would be `unsigned short length;`. And [Do not cast the return value of `malloc()`](http://stackoverflow.com/a/605858/1983495), but do check that it's not `NULL`. Please explain NO BIG EFFORT!, what is your idea, have you tried anything at all? – Iharob Al Asimi May 27 '15 at 11:39
  • My first idea was to cast my array to my struct that way: Packet = (struct Packet*)buffer; This is a really short possibility, but I think it only works, when entries in the struct have the same data type, right? – florian2840 May 27 '15 at 11:45
  • I don't know whether that is a bad idea or not, have to see the code. – Iharob Al Asimi May 27 '15 at 11:46
  • why char byte1[1] make no sense? How can I save a char value from an array in a struct, which is defined as follows: struct Pointer{ char byte1; char byte2;} *Pointer; This does not work: Pointer->byte = array[0]; – florian2840 May 27 '15 at 12:52
  • Why doesn't it work? it sure does, unless your code has some other problem. I think that you are not posting the right question, and that means that you have no clue of what is wrong with your program, and that means that you need to try and **figure out** that by yourself and when you're stuck with an **specific** problem, then you can go to Stack Overflow and ask, meanwhile I am voting to close this question. – Iharob Al Asimi May 27 '15 at 12:55

1 Answers1

1

Firstly fix the struct definition:

typedef struct Packet {
   unsigned char byte1, byte2, byte3;
   unsigned short length;
   unsigned char data[];
} Packet;   // note: no bogus star

Here is one way of reading it in portably:

unsigned char header[5];
if ( 5 != read(fd, header, 5) )
    // error handling... 

unsigned short length = header[3] * 0x100 + header[4]; // assuming network byte order
Packet *packet = malloc( sizeof *packet + length );
if ( !packet )
    // error handling....

packet->byte1 = header[0];
packet->byte2 = header[1];
packet->byte3 = header[2];
packet->length = length;
ssize_t num_read = read(fd, packet->data, length);

if ( num_read != length )
    // error handling...
M.M
  • 138,810
  • 21
  • 208
  • 365
  • I dont understand thos two lines: unsigned short length = header[3] * 0x100 + header[4]; // assuming network byte order Packet *packet = malloc( sizeof *packet + length ); I thought I have to set the length of my data array. And the length of my data array is standing in dataLength. How I can set the length? – florian2840 Jun 01 '15 at 07:02
  • @florian2840 I am using the technique [flexible array member](http://stackoverflow.com/questions/3047530/flexible-array-member-in-c-structure) where you call `malloc` once; and that block will contain the data, and `byte1` `byte2` and `byte3`. My suggestion is that this is simpler than your setup of having to malloc a `Packet` and then also malloc more space for data. – M.M Jun 01 '15 at 07:53
  • `header[3] * 0x100 + header[4]` converts the two bytes stored in your `lengthData` to be an integer value. For example of those bytes were `0x02 0x30` then the length will be `0x230` (or `560` decimal). You didn't say which of those two bytes was the [most significant byte](http://stackoverflow.com/a/16535398/1505939) so I made the assumption that the first one is ; as that is the most common way to send integers over a network. – M.M Jun 01 '15 at 07:54