簡體   English   中英

C的命名約定:結構和函數

[英]The Naming Conventions of C: A struct and a function

sigaction.h頭文件中探索處理系統信號請求的工具時,我注意到返回int的結構和函數被命名為sigaction

即使它看起來在語義上是正確的,因為編譯器應該能夠在兩個定義之間進行推導,為什么sigaction的重復定義是有效的C語法?

在C中,struct標簽與其他名稱位於不同的名稱空間中。 該結構稱為struct sigaction ,而該函數只是sigaction

編譯器能夠分離結構標記(也是聯合和枚舉),因為它們分別遵循關鍵字struct / union / enum (C11§6.2.3p1)。

然后,根據§6.7p3,聲明在“同名空間”內必須是唯一的。

由於結構標記和函數標識符( 普通標識符 )不在同一名稱空間中,因此“沖突”是可以的。

在使用方面,您無法做到:

typedef struct _test {
    int a;
} test;

void test(void) {

}

編譯器會通知您:

test.c:5:6: error: 'test' redeclared as different kind of symbol
 void test(void) {
      ^
test.c:3:3: note: previous declaration of 'test' was here
 } test;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM