与scanf(%ms)一起使用strlen

时间:2018-08-05 10:27:06

标签: c scanf dynamic-memory-allocation c-strings strlen

是否可以在动态分配的字符串上使用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;
}

1 个答案:

答案 0 :(得分:6)

您可以在以install.packages("magrittr") library(magrittr) library(dplyr) library(ggplo2) cars_filtered <- Cars_csv %>% filter(Cylinders == 4 | Cylinders == 6) Cars_csv %>% boxplot(cars_filtered ~ Cars_csv$Economy_highway) 结尾的strlen()的任何序列char上使用'\0',空字符又名NUL * 1 ,实际上等于0

内存分配方式无关紧要。

是的,这也适用于“ 动态分配的”内存。


* 1:不能与空指针常量NULL混用。