我想了解《標準》中的一些例子是什么意思。
N2346::6.7.2.3/p12(emp. mine):
示例2為了說明使用標簽的事先宣告來指定一對相互參照的結構,宣告
struct s1 { struct s2 *s2p; /* . .. */ }; // D1 結構 s2 { struct s1 *s1p; /* . .. */ }; // D2指定一對包含彼此指標的結構。 注意,如果
。s2已經被宣告為中的一個標記,那么 封閉的作用域中,宣告D1將指代它,而不是指代標記。 標簽s2在D2中宣告。為了消除這種背景關系的敏感性,宣告 宣告struct s2;可以在
D1前面插入。這宣告了一個新標簽s2在 然后宣告D2就完成了對新型別的規范。 新型別的規范。
我不太明白強調的部分是什么意思。
如果一個標簽s2將在包圍的范圍內被宣告,例如如下:
struct s2; /previous visible declaration
struct s1 { struct s2 *s2p; /* . .. */ }; // D1
結構 s2 { struct s1 *s1p; /* . .. */ }; // D2
那么宣告和定義都將指定相同的型別。
N2346::6.7.2.3/p8:
如果一個形式為的型別指定器struct-or-union identifier或者
enum identifier除了作為上述形式的一部分之外,還出現了一個宣告 識別符的宣告是可見的,那么它指定的型別與其他宣告相同。 與其他宣告的型別相同,并且沒有重新宣告標簽。
而型別是結構s2 { struct s1 *s1p; /* ... */ };定義在D2。
那么,通過在D1前面插入宣告來消除背景關系敏感性,標準是什么。我在這里沒有看到任何背景關系敏感性。
uj5u.com熱心網友回復:
你創建的例子只有一個范圍。 為了演示他們的觀點,你需要有一個嵌套的范圍。 例如,下面的完整程式演示了這一點:
#include <stdio.h>/span>
struct foo {
int a;
};
int main(void)
{
struct bar {
int b;
struct foo *ptr;
};
struct foo {
int c;
struct bar *ptr; /span>
};
struct bar x;
printf("%zu%zu
", sizeof(struct foo), sizeof(*x.ptr) )。
return 0;
}
當我在我的系統上運行這個時,我得到:
16 4
這兩個尺寸不一樣的原因是第一個尺寸來自于struct foo的本地定義,而struct bar使用的是之前宣告的struct foo的全域定義,因為struct bar的定義在struct foo的本地定義之前。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/322486.html
標籤:
