1

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.

Libor Zapletal
  • 13,752
  • 20
  • 95
  • 182
  • The dns query received via `recvfrom` does not contain pointer values, so you cannot map it directly to the struct you showed in the question. Either you need to modify your struct definitions not to contain pointers, or you need to parse the received data, and fill the struct field by field. – Sander De Dycker Nov 18 '12 at 10:14

1 Answers1

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; 
}; 
Will
  • 4,585
  • 1
  • 26
  • 48
  • Thanks, it helped and could you help me once more with this? Now when I tried that printf I get seg fault 11. Where could be this problem? – Libor Zapletal Nov 18 '12 at 09:48
  • I don't think your structure maps onto the data you are casting properly. looks like one if the pointers you dereference is invalid (possibly null or similar). try printing each pointer before you dereference, or use a debugger to inspect their values. – Will Nov 18 '12 at 09:53
  • Also you should probably remember that `foo * bar` and `foo bar[]` are not the same thing. The first is a pointer to some memory, the second is a piece of memory that contains more than one of the type `foo` next to each other. – Will Nov 18 '12 at 09:57
  • Thanks, I know difference. I tried and `dnsQuery->querySection` isn´t null but when I tried to access variables in querySection I got seg fault. Maybe some problem with `unsigned char *name` in querySection but how can I fix it? It has variable lenght. – Libor Zapletal Nov 18 '12 at 10:11
  • Just because the pointer doesn't equal NULL doesn't mean that the pointer is valid. NULL is just guaranteed to be invalid. If you are getting the data from a file or buffer or something then You need to write a structure that mimics it's representation in that buffer. E.g. a list of ten characters followed by a list of some structures might be `struct bar { char name[10]; struct baz[]; }`. Without knowing the underlying data I can't really help. – Will Nov 18 '12 at 10:14
  • You may also need to take into account structure padding and packing if dealing with data from files. http://stackoverflow.com/questions/10371296/pragma-pack1-nor-attribute-aligned-1-works – Will Nov 18 '12 at 10:22