C - byte array to structure (dns query)

ぐ巨炮叔叔 提交于 2019-12-04 05:10:43

问题


I have these structures:

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;

and I have dns query in byte array from recvfrom. I am trying to get structure from byte array like this:

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

Why I get error Dereferencing pointer to incomplete type? Am I doing this right? Or how can I get dns query structure from that array? I need that dns query question and type.


回答1:


your query section printer is an incomplete type. you need to either typedef it beforehand and not use the structure keyword or use the structure name rather than the typedef. e.g.:

typedef struct foo Foo;

struct {
    Foo* querySection;
    // effectively same as above
    struct foo* querySection2;

    // NOT the following. 
    struct Foo* querySectionWrong; 
}; 


来源:https://stackoverflow.com/questions/13438969/c-byte-array-to-structure-dns-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!