A
Angus
Here is the code:
void FillHeader(char* buf, size_t len)
{
*--buf = len;
*--buf = len>>8;
}
int main(){
//this buffer is of form where first two bytes denote data size,
data follows
char buf[8]; //buffer to use
buf[0] = 0; //size fields
buf[1] = 0;
buf[2] = '1'; //start of data
buf[3] = '2';
buf[4] = '3';
buf[5] = '4';
buf[6] = '5';
buf[7] = '6';
//we specify start of data and size of data
FillHeader(&buf[2], 6);
//Next test will be where buffer has first 2 bytes with header
value. Size of data goes in bytes 2 and 3
char buf2[10]; //buffer to use
buf2[0] = 127; //header bytes
buf2[1] = 127;
buf2[2] = 0; //size fields
buf2[3] = 0;
buf2[4] = '1'; //give some data
buf2[5] = '2';
buf2[6] = '3';
buf2[7] = '4';
buf2[8] = '5';
buf2[9] = '6';
FillHeader(&buf2[4], 6);
return 0;
}
It works fine but I don't really understand the FillHeader function.
The *--buf specifically. How does this work?
void FillHeader(char* buf, size_t len)
{
*--buf = len;
*--buf = len>>8;
}
int main(){
//this buffer is of form where first two bytes denote data size,
data follows
char buf[8]; //buffer to use
buf[0] = 0; //size fields
buf[1] = 0;
buf[2] = '1'; //start of data
buf[3] = '2';
buf[4] = '3';
buf[5] = '4';
buf[6] = '5';
buf[7] = '6';
//we specify start of data and size of data
FillHeader(&buf[2], 6);
//Next test will be where buffer has first 2 bytes with header
value. Size of data goes in bytes 2 and 3
char buf2[10]; //buffer to use
buf2[0] = 127; //header bytes
buf2[1] = 127;
buf2[2] = 0; //size fields
buf2[3] = 0;
buf2[4] = '1'; //give some data
buf2[5] = '2';
buf2[6] = '3';
buf2[7] = '4';
buf2[8] = '5';
buf2[9] = '6';
FillHeader(&buf2[4], 6);
return 0;
}
It works fine but I don't really understand the FillHeader function.
The *--buf specifically. How does this work?