C语言中内存长度的问题
char str[] = “http://www.renren.com” (长度为21)
char *p = str ;
请计算
sizeof (str ) = ?(1)
sizeof ( p ) = ?(2)
void Foo ( char str[100]){
sizeof( str ) = ?(3)
}
void *p = malloc( 100 );
sizeof ( p ) = ?(4)
结果应该是**== 22,4,4,4 ==**
只要是指针都是4
数组作为形参退化为指针处理,所以(3)处是4
在VC++6.0中输入一下代码
#include<stdio.h>
int Foo( char str[100])
{
return sizeof(str) ;
}
int main()
{
char str[] = "http://www.renren.com";
char *p = str ;
int *q;
q=malloc(100*sizeof(int));
printf("%d\n",sizeof(str));
printf("%d\n",sizeof(p));
printf("%d\n",sizeof(q));
printf("函数调用时str长度等于: %d\n",Foo(str));
printf("%d\n",sizeof(char));
}
输出结果如下图所示: