对于指针,sizeof操作符返回这个指针占的空间,一般是4个字节;而对于一个数组,sizeof返回这个数组所有元素占的总空间。
char*与char[]容易混淆,一定要分清,而且char*="aaa"的写法现在不被提倡,应予以避免。
strlen不区分是数组还是指针,就读到\0为止返回长度。而且strlen是不把\0计入字符串的长度的。
int *p=NULL, sizeof(*p) = sizeof(int) = 4,是说整型数占用几个字节。
某32位系统下, C++程序,请计算sizeof 的值
#include
<
stdio.h
>
#include < malloc.h >
void Foo ( char str[ 100 ] )
{
printf( " sizeof(str)=%d \n " , sizeof (str) );
}
main()
{
char str[] = " www.ibegroup.com " ;
char * p1 = str ;
int n = 10 ;
void * p2 = malloc( 100 );
printf( " sizeof(str)=%d \n " , sizeof (str) );
printf( " sizeof(p1)=%d \n " , sizeof (p1) );
printf( " sizeof(n)=%d \n " , sizeof (n) );
printf( " sizeof(p2)=%d \n " , sizeof (p2) );
Foo(str);
}
#include < malloc.h >
void Foo ( char str[ 100 ] )
{
printf( " sizeof(str)=%d \n " , sizeof (str) );
}
main()
{
char str[] = " www.ibegroup.com " ;
char * p1 = str ;
int n = 10 ;
void * p2 = malloc( 100 );
printf( " sizeof(str)=%d \n " , sizeof (str) );
printf( " sizeof(p1)=%d \n " , sizeof (p1) );
printf( " sizeof(n)=%d \n " , sizeof (n) );
printf( " sizeof(p2)=%d \n " , sizeof (p2) );
Foo(str);
}
答:(1)17 (2)4 (3) 4 (4)4 (5)4