C – 字节数组结构(dns查询)

我有这些结构:

typedef struct dnsQuery { char header[12]; struct TdnsQuerySection *querySection; } TdnsQuery; typedef struct dnsQuerySection { unsigned char *name; struct TdnsQueryQuestion *question; } TdnsQuerySection; typedef struct dnsQueryQuestion { unsigned short qtype; unsigned short qclass; } TdnsQueryQuestion; 

我从recvfrom有字节数组的dns查询。 我试图从字节数组获取结构,如下所示:

 TdnsQuery* dnsQuery = (TdnsQuery*)buf; printf("%u", dnsQuery->querySection->question.qtype); 

为什么我得到错误解除指向不完整类型的指针? 我这样做了吗? 或者如何从该数组中获取dns查询结构? 我需要dns查询问题和类型。

您的查询部分打印机是不完整的类型。 您需要事先键入它并且不使用结构关键字或使用结构名称而不是typedef。 例如:

 typedef struct foo Foo; struct { Foo* querySection; // effectively same as above struct foo* querySection2; // NOT the following. struct Foo* querySectionWrong; };