繁体   English   中英

与scanf(%ms)一起使用strlen

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM