关于sizeof的问题

来源:互联网 发布:事件营销诺一网络公关 编辑:程序博客网 时间:2024/05/22 05:27

假如是32位机 

void Func ( char str[100])

{
    请计算
    sizeof( str ) = 4 (因为数组传给函数时自动转化成了指针)
}
 
char str[] = “Hello” ;
char *p = str ;
int n = 10;
 
请计算
sizeof (str ) = 6 (str是字符数组,加上“/0”一共6个字节)
sizeof ( p ) = 4 (p是指针,就是一个地址,32位机就是4个字节)
 
C++ primer里面写有“In most cases when we use an array, the array is automatically converted to a pointer to the first element;The exceptions when an array is not converted to a pointer are: as the operand of the address-of (&) operator or of sizeof, or when using the array to initialize a reference to the array.”
str使用sizeof后仍然是数组,不会转变成指针
sizeof (*p )  = 1(p为指向第一个元素的指针,*p就是字符H)
sizeof ( n ) = 4 
void *p = malloc( 100 );
请计算
sizeof ( p ) = 4 
 
类似的还有
short a[100]; 
short *b; 
short (*c)[100]; 
short *d[100];
sizeof(a)=200 (100个short型数据,每个占两个字节)
sizeof(b)= 4
sizeof(c)= 4(c为指向一维数组的指针,这个一维数组有100个整型数据)
sizeof(d)=400(指针数组,一共一百个,每个均指向整型数组)
原创粉丝点击