3 回答

TA貢獻(xiàn)1895條經(jīng)驗 獲得超7個贊
typedef會是
typedef char type24[3];
但是,這可能是一個非常糟糕的主意,因為結(jié)果類型是一種數(shù)組類型,但它的用戶不會看到它是一個數(shù)組類型。如果用作函數(shù)參數(shù),它將通過引用傳遞,而不是通過值傳遞,并且sizeoffor它將是錯誤的。
一個更好的解決方案是
typedef struct type24 { char x[3]; } type24;
您可能也希望使用unsigned char而不是char,因為后者具有實現(xiàn)定義的簽名。

TA貢獻(xiàn)1845條經(jīng)驗 獲得超8個贊
你要
typedef char type24[3];
C類型的聲明很奇怪。如果聲明了該類型的變量,則將類型精確地放在變量名稱的位置。

TA貢獻(xiàn)1801條經(jīng)驗 獲得超16個贊
來自R ..的回答:
但是,這可能是一個非常糟糕的主意,因為結(jié)果類型是一種數(shù)組類型,但它的用戶不會看到它是一個數(shù)組類型。如果用作函數(shù)參數(shù),它將通過引用傳遞,而不是通過值傳遞,并且它的sizeof將是錯誤的。
沒有看到它是一個數(shù)組的用戶很可能會寫這樣的東西(失?。?/p>
#include <stdio.h>
typedef int twoInts[2];
void print(twoInts *twoIntsPtr);
void intermediate (twoInts twoIntsAppearsByValue);
int main () {
twoInts a;
a[0] = 0;
a[1] = 1;
print(&a);
intermediate(a);
return 0;
}
void intermediate(twoInts b) {
print(&b);
}
void print(twoInts *c){
printf("%d\n%d\n", (*c)[0], (*c)[1]);
}
它將使用以下警告進(jìn)行編譯:
In function ‘intermediate’:
warning: passing argument 1 of ‘print’ from incompatible pointer type [enabled by default]
print(&b);
^
note: expected ‘int (*)[2]’ but argument is of type ‘int **’
void print(twoInts *twoIntsPtr);
^
并產(chǎn)生以下輸出:
0
1
-453308976
32767
- 3 回答
- 0 關(guān)注
- 663 瀏覽
添加回答
舉報