[英]Use strlen with scanf(%ms)
Is it possible to use strlen()
over a dynamically allocated string? 是否可以在动态分配的字符串上使用
strlen()
?
FOR EXAMPLE : 例如 :
#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;
}
You can use strlen()
on any sequence of char
s ended by a '\\0'
, the null-character aka NUL
*1 , which in fact equals 0
. 您可以在以
'\\0'
结尾的任何char
序列上使用strlen()
,即null字符,即NUL
* 1 ,实际上等于0
。
It does not matter how the memory has been allocated. 内存的分配方式无关紧要。
So yes, this also applies to " dynamically allocated " memory. 是的,这也适用于“ 动态分配 ”的内存。
*1: Not be mixed up with NULL
, which is the null-pointer constant. * 1:不能与
NULL
混合使用, NULL
是空指针常量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.