这对我不起作用:
char buff[11];
char* msg_ptr;
msg_ptr = buff;
uint8_t id;
uint32_t msg_length;
char msg[] = "hallo";
id = 77;
msg_length = 5;
memcpy(buff, &id, sizeof(uint8_t));
memcpy(buff+1, &msg_length, sizeof(uint32_t));
strcpy(buff+5, msg);
printf("id :%d\n",*(uint8_t*)msg_ptr);
msg_ptr++;
printf("msg_length: %d\n", *msg_ptr);
msg_ptr++;
printf("msg: %s\n", msg_ptr);
Id 和 msg_lentgh 显示在 konsole 上,但之后出现分段错误。我是C新手,请尽可能详细。问候