#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void Func(char str_arg[100])
{
printf("%d\n",sizeof(str_arg));
}
int main(void)
{
char str[]="Hello";
printf("%d\n",sizeof(str));
printf("%d\n",strlen(str));
char*p=str;
printf("%d\n",strlen(p));
printf("%d\n",sizeof(p));
Func(str);
}
在64位系统中结果为
6
5
5
8
8
主要易错的地方在于指针以及函数传递。
在32位系统上,不管指针p指向的是整型数据,还是字符型数据,short型数据,long型数据等,指针p本身所占的内存字节数均为4;在64位系统指针字节数为8。
sizeof(p)获取的是指针地址,strlen(p)可以获取数组长度。
数组传递时也类似,虽然看上去printf一个数组,其实还是指针。
所以sizeof的使用需要多加考虑。