I think you need to check the alignment of each member in your struct.
Code:
$ cat bar.c
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
typedef struct meaning {
char gap ;
int address ;
char remaining[5] ;
}meaning;
int main ( ) {
printf("Size=%zd\n", sizeof(meaning));
printf("Offset=%zd\n", offsetof(meaning,address));
return 0;
}
$ gcc bar.c
$ ./a.out
Size=16
Offset=4
On most machines, your struct is neither 10 bytes long, nor is your integer at offset 1.
To pack as you want, you typically need something like a pragma (or attributes in gcc)
Code:
$ cat bar.c
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#pragma pack(1)
typedef struct meaning {
char gap ;
int address ;
char remaining[5] ;
}meaning;
int main ( ) {
printf("Size=%zd\n", sizeof(meaning));
printf("Offset=%zd\n", offsetof(meaning,address));
return 0;
}
$ gcc bar.c
$ ./a.out
Size=10
Offset=1
Whilst this gets the int to the correct place, you'll find that any code which accesses that int will be "slugged" to take into account it's unaligned position in memory.
The most reliable method of solving your problem is something like
Code:
myInt = (int)buff[1] << 24 | (int)buff[2] << 16 | (int)buff[3] << 8 | (int)buff[4];
Which works regardless of endian issues on the host machine, so long as you know the endianess of the input message.
If you know the endian is the same, you can short-cut to
Code:
memcpy( &myInt, &buff[1], sizeof(myInt) );