概述
直接上代码,使用中经常会涉及到sizeof与strlen计算的问题,下面看例子:
char *pstr = "hello";
char pstr[] = "hellonini";
char pstr[6] = "hello";
上述求sizeof(pstr)的值,分别为:4 10 6 。
第一个为求指针所占字节的大小,第二个为数组pstr所占字节的大小,第三个为数组pstr所占字节的大小。
继上述sizeof,strlen求的是字符串的长度,不包含末尾的\0.
char pstr[] = "hello";
char *pstr = "hello world";
strlen(pstr)得到的值为:5 11。