【问题标题】:C programming: scanf for char pointer not workingC编程:char指针的scanf不起作用
【发布时间】:2018-01-03 01:50:26
【问题描述】:

我有一个程序可以接收用户输入的课堂和时间。课堂输入存储在 char* 课堂中,时间存储在 int 时间中。但是,当我运行程序时,它会在我键入教室后按 Enter 时停止。 “请输入时间:”的 printf 没有出现。为什么会这样??

void option1()
{
  char* classroom; 
  int time;        

  printf("Please enter the classroom: ");
  scanf_s("%s", &classroom); 

  printf("Please enter the time: ");
  scanf_s("%d", &time);
}

感谢您的帮助:)

【问题讨论】:

  • scanf_s 工作方式不同,您正在寻找scanf
  • classroom 已经是一个指针,这里不要使用& 运算符。
  • "%s" 是保证缓冲区溢出,始终使用字段宽度。
  • classroom 没有指向任何地方,你没有为它分配内存 -> 未定义的行为
  • @FelixPalmen 和课堂点 where = undefined behavior?啊,更快:)

标签: c char printf scanf char-pointer


【解决方案1】:

正如我已经评论过的,这个sn-p有很多错误:

  • scanf_sscanf 不同,它需要额外的大小参数。我建议不要使用它。
  • 永远不要只使用"%s"scanf(),您需要指定字段宽度以防止缓冲区溢出。 (这对于scanf_s() 是不同的,因为缓冲区大小是那里的一个附加参数。)
  • 你试图通过一个不指向任何地方的指针(classroom)写入数据,你需要分配内存! (通过将其设为数组或调用malloc())。

纠正了这些错误的 sn-p可能如下所示:

void option1()
{
  char classroom[128];
  int time;        

  printf("Please enter the classroom: ");
  scanf("%127s", classroom);
  // field width is one less than your buffer size,
  // because there will be a 0 byte appended that terminates the string!

  printf("Please enter the time: ");
  scanf("%d", &time);
}

【讨论】:

    【解决方案2】:

    要么创建变量作为指针并为其分配内存,要么分配char数组并将其传递给scanf。

    //Use of array
    char classroom[10];
    scanf("%9s", classroom); 
    
    //Use of heap
    char* classroom = malloc(10 * sizeof(*classroom));
    scanf("%9s", classroom); 
    //Use your variable classroom here, when done, call free to release memory.
    free(classroom);
    

    9 在这里作为字符串长度,以防止缓冲区溢出和越界写入。数组的大小为10 个元素。

    【讨论】:

      【解决方案3】:

      使用scanf而不是scanf_s,为char指针分配内存并且不要使用&,它已经是一个指针。请参见下面的示例:

      #include <stdio.h>
      #include <stdlib.h>
      
      int main()
      {
        int time;        
        char* classroom; 
        classroom = malloc(sizeof(char) * 1024);
      
        if(classroom == NULL)
        {
          fprintf(stderr, "Failed to allocate memory!\n");
          exit(1);
        }
      
        printf("Please enter the classroom: ");
        scanf("%s", classroom); 
      
        printf("Please enter the time: ");
        scanf("%d", &time);
      
      
        printf("\nClassroom: %s\n", classroom);
        printf("Time: %d\n", time);
      
        free(classroom); //Free the memory
      
        return 0;
      }
      

      【讨论】:

        【解决方案4】:

        scanf 系列函数将格式字符串指定的数据读取到作为参数提供的变量中。

        这些函数为字符串变量分配存储空间!在没有为其分配内存的情况下向 scanf 提供变量 classroom,将使 scanf 尝试将数据放置在内存中未定义的位置。这可能导致任何类型的行为,通常称为未定义行为

        所以你必须先为你的字符串变量分配存储空间,例如:

        char*classroom= malloc(1024);
        

        现在你可以用它调用 scanf 了:

        scanf_s("%s", classroom);
        

        请注意,由于 char 指针的行为类似于数组(即它指向 char 数组),所以不要使用运算符的地址 &amp;

        【讨论】:

        • hmm,malloc部分有问题:严重性代码描述项目文件行抑制状态错误(活动)“void *”类型的值不能用于初始化“char”类型的实体*"
        • @Daaenerys,您可能编译为 c++。在 c 中这很好。
        猜你喜欢
        • 2014-11-10
        • 2020-06-30
        • 1970-01-01
        • 2015-06-12
        • 1970-01-01
        • 2015-09-27
        • 1970-01-01
        • 2021-11-26
        • 2019-07-04
        相关资源
        最近更新 更多