C/C++隨筆 之 C++ Sizeof()函數(shù)
原文出處:
http://community.csdn.net/Expert/topic/5253/5253878.xml?temp=.5134088
A?====char *p[10];int *a[10];
針對這個問題
首先我給出另一個例子
B?====char (*p)[10];int (*a)[10];
A和B是不一樣的
A解釋:char *p[10]定義了10個32位的指向char的指針p[0],p[1],p[2]......p[9],
??????int *a[10]定義了10個32位的指向int的指針a[0],a[1],a[2]......a[9],
Get:10 * 32 /8 = 40,OK
B解釋:char (*p)[10]定義了一個指向10個char的32位指針p
??????int (*a)[10]定義了一個指向10個int的32位指針a
Get:32/ 8 = 4, OK
???????????????
Code Example:
void main()
{
?char *p[10];
?int? *a[10];
?char (*p1)[10];
?int? (*a1)[10];
?int x1 = sizeof(p);
?int x2 = sizeof(a);
?int x3 = sizeof(p1);
?int x4 = sizeof(a1);
?cout <<x1<<"\n"<<x2<<"\n"<<x3<<"\n"<<x4<<"\n";
}
http://community.csdn.net/Expert/topic/5253/5253878.xml?temp=.5134088
A?====char *p[10];int *a[10];
針對這個問題
首先我給出另一個例子
B?====char (*p)[10];int (*a)[10];
A和B是不一樣的
A解釋:char *p[10]定義了10個32位的指向char的指針p[0],p[1],p[2]......p[9],
??????int *a[10]定義了10個32位的指向int的指針a[0],a[1],a[2]......a[9],
Get:10 * 32 /8 = 40,OK
B解釋:char (*p)[10]定義了一個指向10個char的32位指針p
??????int (*a)[10]定義了一個指向10個int的32位指針a
Get:32/ 8 = 4, OK
???????????????
Code Example:
void main()
{
?char *p[10];
?int? *a[10];
?char (*p1)[10];
?int? (*a1)[10];
?int x1 = sizeof(p);
?int x2 = sizeof(a);
?int x3 = sizeof(p1);
?int x4 = sizeof(a1);
?cout <<x1<<"\n"<<x2<<"\n"<<x3<<"\n"<<x4<<"\n";
}
posted on 2006-12-25 13:13 木木頭 閱讀(221) 評論(0) 編輯 收藏 引用 所屬分類: C++特性