关于sizeof的问题
2008-05-20 16:15
323 查看
假如是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(指针数组,一共一百个,每个均指向整型数组)
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(指针数组,一共一百个,每个均指向整型数组)
相关文章推荐
- 关于结构体struct的sizeof计算问题
- 关于sizeof的几个问题
- 关于类对象大小的 sizeof 计算问题
- 关于结构体的内存分配问题--sizeof(/*struct*/A)
- 关于sizeof的问题(2)——求结构体大小
- 关于类的sizeof问题
- 关于c语言字符串中sizeof指针的问题
- 关于sizeof注意的问题
- 关于类对象大小的 sizeof 计算问题(续)
- 关于类对象大小的 sizeof 计算问题
- sizeof(struct)--关于字节对齐的问题
- 问题-关于SizeOf在Delphi7和Delphi2009下结果分别是16/32
- 关于sizeof和strlen()那些年我们都迷惑过的问题
- 关于sizeof的相关问题
- 关于sizeof(struct student)的问题
- 转:struct 关于sizeof的大小-内存对齐问题
- 关于nest class 的构造问题 和 sizeof 问题
- 关于使用memset(a,0xff,sizeof(a)),输出-1的问题
- 关于类对象大小的 sizeof 计算问题
- 关于数组做形参,用sizeof测长度的问题