结构体格式如下:
struct ss_t
{
short int totallen;
short int command;
char msg[0];
};
需要给结构体中的msg赋值,由于msg的长度为0,直接赋值时程序提示赋值错误,经过处理之后,可以使用以下方式赋值:
int totallen;
int command;
char msg[] = "123456";
int testlen = sizeof(msg);
char *buf;
struct ss_t *st;
buf = (char *)malloc(sizeof(char) * 100);
if (buf == NULL) {
return ;
}
memset(buf, 0, 100);
totallen = strlen(msg);
command = 1000;
memcpy(buf, &totallen, sizeof(short int));
memcpy(buf + sizeof(short int), &command, sizeof(short int));
memcpy(buf + 2 * sizeof(short int), msg, strlen(msg));
st = (struct ss_t *)buf;
char showres[50]={0};
char showmsg[50]={0};
sprintf(showres,"st->s_id %d st->len %d\n", st->totallen, st->command);
sprintf(showmsg,"st->msg %s\n", st->msg);
MessageBoxA(NULL,showres,showmsg,0);