3 回答

TA貢獻(xiàn)1712條經(jīng)驗 獲得超3個贊
類型void沒有大??;那將是一個編譯錯誤。出于同樣的原因,您不能執(zhí)行以下操作:
void n;
編輯。令我驚訝的是,sizeof(void)實際上在GNU C中確實可以編譯:
$ echo 'int main() { printf("%d", sizeof(void)); }' | gcc -xc -w - && ./a.out
1
但是,在C ++中,它不會:
$ echo 'int main() { printf("%d", sizeof(void)); }' | gcc -xc++ -w - && ./a.out
<stdin>: In function 'int main()':
<stdin>:1: error: invalid application of 'sizeof' to a void type
<stdin>:1: error: 'printf' was not declared in this scope

TA貢獻(xiàn)1804條經(jīng)驗 獲得超8個贊
如果使用的是GCC,并且沒有使用刪除編譯器特定擴(kuò)展的編譯標(biāo)志,sizeof(void)則為1。GCC具有執(zhí)行此操作的非標(biāo)準(zhǔn)擴(kuò)展。
通常,void它是不完整的類型,您不能將sizeof用于不完整的類型。
- 3 回答
- 0 關(guān)注
- 602 瀏覽
添加回答
舉報