[英]Use strlen with scanf(%ms)
是否可以在动态分配的字符串上使用strlen()
?
例如 :
#include <stdio.h>
#include <string.h>
int main ()
{
char *input=NULL;
printf ("Enter a sentence: ");
scanf("%ms", &input);
//Is this legit?
printf ("The sentence entered is %u characters long.\n",(unsigned)strlen(input));
return 0;
}
您可以在以'\\0'
结尾的任何char
序列上使用strlen()
,即null字符,即NUL
* 1 ,实际上等于0
。
内存的分配方式无关紧要。
是的,这也适用于“ 动态分配 ”的内存。
* 1:不能与NULL
混合使用, NULL
是空指针常量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.