如果你涉足网络传输方向的开发,我想你对这段类似的代码应该不会很陌生,先看代码:
int check_msg(svr_proto_t* pkg, uint32_t bodylen, fdsession_t* fdsess)
{
struct report_msg {
uint32_t gameid;
uint32_t userid;
uint32_t recvid;
uint32_t onlineid;
uint32_t maptype;
uint32_t mapid;
uint32_t timestamp;
uint32_t msglen;
char msg[];
}__attribute__((packed));
if (bodylen <= sizeof(report_msg)) {
KERROR_LOG(pkg->id, "invalid len\t[%u]", bodylen);
return 0;
}
report_msg* pmsg = (report_msg *)pkg->body;
if (pmsg->msglen + sizeof(report_msg) != bodylen) {
KERROR_LOG(pkg->id, "invalid len\t[%u %u]", bodylen, pmsg->msglen);
return 0;
}
//TODO other logic
return 0;
}
这里你会看到有report_msg 这个结构体,他的msg成员是一个数组,数组长度是0,当然你也可以写成 c