here I want to read data from input to struct dev, so I am using temp = (dev*) input to casting from char* to dev*. On my machine,the line printf("%p\n", temp); prints out 0x804a040, and if is true the line printf("%p\n", temp+1); should prints out 0x804a04b, but it printed 0x804a04c. the line printf("%p\n", &temp->size); should prints out 0x804a047, but it printed 0x804a048.
Plz help me figure out what I misunderstand or incorrect
@all: thanks all for your helping. I got it. I've read some post and solusion is using #paragma pack(1) or some directive like this. but it also is discourage because it will slow the processing of precessor. So, is there any other ideas for this.
Thank!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct dev
{
short id;
char name[5];
int size;
} dev;
char input[] = { 0x03, 0x00, 0x65, 0x67, 0x03, 0x00, 0x43, 0x09, 0x00, 0x00, 0x00,
0x00, 0x00, 0x65, 0x67, 0x03, 0x00, 0x43, 0x43, 0x00, 0x43, 0x00,
0x03, 0x00, 0x65, 0x67, 0x03, 0x00, 0x43, 0x43, 0x00, 0x43, 0x00,
0x03, 0x00, 0x65, 0x67, 0x03, 0x00, 0x43, 0x43, 0x00, 0x43, 0x00 };
int
main (int argc, char *argv[])
{
dev* temp;
temp = (dev*)input;
printf("%p\n", temp);
printf("%p\n", temp+1);
printf("%d\n", temp->id);
printf("%p\n", &temp->name[4]);
printf("%p\n", &temp->size);
temp++;
printf("%d\n", temp->id);
return 0;
}