2012-11-18 45 views
1

我有這些結構:Ç - 字節數組結構(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查詢問題和類型。

+0

通過'recvfrom'收到的dns查詢不包含指針值,所以你不能直接將它映射到你在問題中顯示的結構。要麼你需要修改你的結構體定義不要包含指針,要麼你需要解析接收到的數據,並按字段填充結構體。 –

回答

1

您的查詢部分打印機是不完整的類型。您需要事先鍵入def,而不要使用結構關鍵字或使用結構名稱而不是typedef。例如:

typedef struct foo Foo; 

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

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

謝謝,它幫助,你能再次幫助我嗎?現在當我嘗試printf時,我得到了seg fault 11.哪裏可能出現這個問題? –

+0

我不認爲你的結構映射到正確投射的數據上。看起來像一個,如果您解除引用的指針無效(可能爲null或類似)。嘗試在取消引用前打印每個指針,或使用調試器檢查其值。 – Will

+0

另外你應該記住'foo * bar'和'foo bar []'是不一樣的。第一個是指向某個內存的指針,第二個是一個包含多個類型爲'foo'的內存。 – Will