幾個容易混淆的 pointer宣告與大小

請問以下宣告pointer佔多少bytes, 假設 1 pointer: 4bytes
0) char *x;
1) char **a;             //This is a pointer to a pointer to char
2) char *b[20];      //This is an array of 20 pointers to char.
3) char (*c)[20];    //This is a pointer to an array of 20 chars.
4) char (*d)[20][40];

我寫了程式來驗證:
vim main.c

#include

main()
{
char *x;
char **a;
char *b[20];
char (*c)[20];
char (*d)[20][40];

printf ("sizeof(x):%d,sizeof(a):%d,sizeof(b):%d,sizeof(c):%d,sizeof(d):%d\n", sizeof(x),sizeof(a),sizeof(b),sizeof(c),sizeof(d));
}


編譯程式
gcc main.c

執行產生的目標檔與結果
# ./a.out
sizeof(x):4,sizeof(a):4,sizeof(b):80,sizeof(c):4,sizeof(d):4

分類: 未分類

發表迴響