草庐IT

利用 strlen 和 sizeof 求取字符串长度注意事项

首先,strlen是函数,sizeof是运算操作符,二者得到的结果类型为size_t,即unsignedint类型。大部分编译程序在编译的时候就把sizeof计算过了,而strlen的结果要在运行的时候才能计算出来。对于以下语句:char*str1="asdfgh";charstr2[]="asdfgh";charstr3[8]={'a','s','d'};charstr4[]="as\0df";执行结果是:sizeof(str1)=4;strlen(str1)=6;sizeof(str2)=7;strlen(str2)=6;sizeof(str3)=8;strlen(str3)=3;size

利用 strlen 和 sizeof 求取字符串长度注意事项

首先,strlen是函数,sizeof是运算操作符,二者得到的结果类型为size_t,即unsignedint类型。大部分编译程序在编译的时候就把sizeof计算过了,而strlen的结果要在运行的时候才能计算出来。对于以下语句:char*str1="asdfgh";charstr2[]="asdfgh";charstr3[8]={'a','s','d'};charstr4[]="as\0df";执行结果是:sizeof(str1)=4;strlen(str1)=6;sizeof(str2)=7;strlen(str2)=6;sizeof(str3)=8;strlen(str3)=3;size