#include <stdio.h>
#include <malloc.h>
void Func (char *st)
{
//请计算
st =(char*)malloc(sizeof(char)*10);
printf("%d",sizeof( st ));
}void main()
{
char p[]="dfds";
printf("%d",sizeof( p ));
Func(p);
}
请问为什么两个打印结果不同?
#include <malloc.h>
void Func (char *st)
{
//请计算
st =(char*)malloc(sizeof(char)*10);
printf("%d",sizeof( st ));
}void main()
{
char p[]="dfds";
printf("%d",sizeof( p ));
Func(p);
}
请问为什么两个打印结果不同?
#include <malloc.h>
void Func (char *st)
{
//请计算
st =(char*)malloc(sizeof(char)*10);
printf("%d",sizeof( st ));
}void main()
{
char p[]="dfdasdfs";
char *q = NULL;
printf("%d",sizeof( p ));
Func(q);
delete q;
}
改了一下,我的问题为什么一个是4一个是9
字符串在内存中是以一个0字节结尾的.也就是通常所说的'\0'或null.
sizeof()函数是检测参数以字节为单位的大小.所以sizeof("dfds")等于5.
正确的计算字符串长度应该用strlen()函数
#include <malloc.h>
char * Func (char *st)
{
//请计算
st =(char*)malloc(sizeof(char)*10);
printf("%d",sizeof( st ));
return st;
}
改了一下,我的问题为什么一个是4一个是9void main()
{
char p[]="dfdasdfs";
char *q = NULL;
printf("%d",sizeof( p ));
q = Func(q);
delete q;
}
#include <malloc.h>
char * Func (char *st)
{
//请计算
st =(char*)malloc(sizeof(char)*10);
printf("q传到st字符串指针的大小是%d\n",sizeof( st ));
return st;
}
void Func2 ( char str[100])
{
printf("p数组传入str后的大小是%d\n",sizeof( str ));
}
void main()
{
char p[]="dfdasdfs";
char *q = NULL;
printf("p数组的大小是%d\n",sizeof( p ));
printf("q字符串指针的大小是%d\n",sizeof( q ));
q = Func(q);
Func2(p);
delete q;
}
1、sizeof("dfds")
2、sizeof(char *)