一個有趣的問題;我自己已經墮落了。 mostly due to horrid hacks involving dubious type casts on innocent memory-blocks。
這個答案是真的只是一個推論R's答案,其中相當多的釘子的問題(雖然我不十分肯定的and goes out of scope after that function ends..
位)
對我來說,關鍵就在這是細讀:
(1)ISO C99 6.7.2:
兩種類型的具有兼容的類型,如果其類型是相同的。額外的 確定兩種類型是否兼容的規則在類型說明符中的6.7.2中描述爲 ,在類型限定符中描述了6.7.3中的 ,而在描述符中的描述中描述了 .6.5)此外,兩種結構,聯合或枚舉類型如果它們的標籤和成員滿足以下要求,則在單獨的翻譯單位中聲明的文件是兼容的 :如果一個 用標籤聲明,則另一個應使用相同的標籤聲明。 ...
(2)C namespaces
總之,這裏的一些代碼(〜一對夫婦的翻譯單位)希望演示了一些可能奇怪行爲,對於那些還沒有之前打這個問題:
等等。C:
#include <stdio.h>
struct bar {int a; int b;} stbar;
struct bar_ {int a; int d;} stbar_;
void foo(struct bar* pst);
void foo_(struct bar st);
void callfoo()
{
/*no warnings; possibly surprising results ! */
stbar.a=313;
stbar.b=31313;
foo(&stbar);
printf("called foo() with stbar: %d, %d\n", stbar.a, stbar.b);
/*generates incompatible types warnings:
blah.c:23:5: warning: passing argument 1 of ‘foo’ from incompatible pointer type [enabled by default]
blah.c:6:6: note: expected ‘struct bar *’ but argument is of type ‘struct bar_ *’ */
stbar_.a=313;
stbar_.d=31313;
foo(&stbar_);
printf("called foo() with stbar_: %d, %d\n", stbar_.a, stbar_.d);
/*generates incompatible types warnings:
blah.c:31:5: warning: passing argument 1 of ‘foo’ from incompatible pointer type [enabled by default]
blah.c:6:6: note: expected ‘struct bar *’ but argument is of type ‘struct bar *’ */
struct bar {float s; float t;} stbar;
foo(&stbar);
printf("called foo() with locally defined stbar: %f, %f\n", stbar.s, stbar.t);
}
void callfoo_()
{
stbar.a=313;
stbar.b=31313;
//passing in incompatible type by value ~ no warnings; possibly surprising results !
foo_(stbar);
/*uncomenting next line generates compiler error:
blah.c:47:5: error: incompatible type for argument 1 of ‘foo_’
blah.c:7:6: note: expected ‘struct bar’ but argument is of type ‘struct bar_’ */
//foo_(stbar_);
}
void main()
{
callfoo();
callfoo_();
}
blah_.c:
#include <stdio.h>
struct bar {int x; float z;} stbar;
void foo(struct bar* pst)
{
printf("foo : %d, %f\n", pst->x, pst->z);
pst->x=13;
pst->z=13.13;
}
void foo_(struct bar st)
{
printf("foo_ : %d, %f\n", st.x, st.z);
st.x=13;
st.z=13.13;
}
輸出:
$ gcc blah.c blah_.c
blah.c: In function ‘callfoo’:
blah.c:23:5: warning: passing argument 1 of ‘foo’ from incompatible pointer type [enabled by default]
blah.c:6:6: note: expected ‘struct bar *’ but argument is of type ‘struct bar_ *’
blah.c:31:5: warning: passing argument 1 of ‘foo’ from incompatible pointer type [enabled by default]
blah.c:6:6: note: expected ‘struct bar *’ but argument is of type ‘struct bar *’
$ ./a.out
foo : 313, 0.000000
called foo() with stbar: 13, 1095898235
foo : 313, 0.000000
called foo() with stbar_: 13, 1095898235
foo : 13274075, 0.000000
called foo() with locally defined stbar: 0.000000, 13.130000
foo_ : 313, 0.000000
$ gcc --version
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 ...
推論的推論:好評,爲C++ namespace
S中的神。
請發佈此編譯錯誤的最小再現代碼 –
除了一個最小的完整示例再現錯誤,它可能有助於知道這是哪個版本的GCC。它是否包含某些發行版的快照/預發行版?警告風格看起來相當近。 – Flexo
這兩個地方的'struct netif'的定義可能是不同的嗎? – pmg