2012-12-20 28 views
-1

考慮下面的代碼:結構/聯合中的X和X類型有何不同?

struct blah { 
    int x; 
    int y; 
}; 

struct foo { 
    union { 
     struct { 
      struct blah *b; 
     }; 
    }; 
}; 

int main() 
{ 
    struct foo f; 
    struct blah *b; 

    // Warning on below assignment 
    b = &f.b; 
    return 0; 
} 

爲什麼GCC產生assignment from incompatible pointer type警告,儘管兩者LHS和RHS是相同類型的(顯然)的? IOW,當struct blah嵌套在struct foo內時會發生什麼變化?

如果這裏有一個有效的警告,它是什麼?

+0

你試過'b = f.b'嗎? – WhozCraig

回答

3

b = &f.b;試圖分配blah**b。使用b = f.b;代替

1
struct blah { 
    int x; 
    int y; 
}; 

struct foo { 
    union { 
     struct { 
      struct blah b; 
     }; 
    }; 
}; 

你引用b這已經是一個指針。

相關問題