1.sizeof
//cpu 32位下
#include <stdio.h>
#include <string.h>
char str[] = "Hello"; //sizeof(str) = 6
struct size_b{
float f;
char p;
}block; //sizeof(block) = 20 //4+4+3*4=20
struct flag_s1{
char ch, *ptr;
union{
short a, b;
}u;
struct flag_s1 *next;
}s1; //sizeof(s1) = 16 //4(ch)+4(ptr)+2(a)+2(b)+4(next)=16
void func(char *fstr) //sizeof(fstr) = 4
{
printf("sizeof(fstr) = %d\n",sizeof(fstr));
printf("strlen(fstr) = %d\n",strlen(fstr));
//strlen(fstr) = 5 //strlen是求字符串的长度,不包括字符串的‘\0’,所以他的值比sizeof的值小一
}
int main(void) {
printf("sizeof(str) = %d\n",sizeof(str));
printf("sizeof(block) = %d\n",sizeof(block));
printf("sizeof(s1) = %d\n",sizeof(s1));
func(str);
int *p=malloc(100); //sizeof(p) = 4;
return 0;
}
2.strlen
注意:遇到’\0’停止