简体   繁体   English

C编程:char指针的scanf无法正常工作

[英]C programming: scanf for char pointer not working

I have a program which takes in the user input for classroom and time. 我有一个程序可以接收用户输入的教室和时间。 The classroom input is store inside a char* classroom, and the time is store inside a int time. 课堂输入存储在char *教室内部,时间存储在int时间内部。 However, when I run the program, it stops at when I press enter after I have type a classroom. 但是,当我运行该程序时,键入教室后,按Enter键时该程序停止。 The printf for "Please enter a time: ", did not come out. “请输入时间:”的printf没有出来。 Why is this so?? 为什么会这样呢?

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

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

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

Thanks for help :) 感谢帮助 :)

As I already commented, there are a lot of errors in this snippet: 正如我已经评论过的,此代码段中存在很多错误:

  • scanf_s is by no means the same as scanf , it needs additional size parameters. scanf_sscanf ,它需要其他大小参数。 I'd suggest to not use it. 我建议不要使用它。
  • Never use just "%s" with scanf() , you need to specify a field width to prevent a buffer overflow. 切勿在scanf()仅使用"%s" ,您需要指定字段宽度以防止缓冲区溢出。 (This is different for scanf_s() because the buffer size is an additional parameter there.) (这与scanf_s()不同,因为缓冲区大小是那里的一个附加参数。)
  • You try to write data through a pointer ( classroom ) that points nowhere, you need to allocate memory! 您尝试通过无处指向的指针( classroom )写入数据,您需要分配内存! (either by making this an array or by calling malloc() ). (通过将其设置为数组或通过调用malloc() )。

Your snippet with these errors corrected might look like: 纠正了这些错误的代码段可能如下所示:

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);
}

Either create variable as pointer and allocate memory for it or allocate char array and pass it to scanf. 创建变量作为指针并为其分配内存,或者分配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 is here as string length to prevent buffer overflow and out of bounds writing. 此处的字符串长度为9以防止缓冲区溢出和越界写入。 Array has size of 10 elements. 数组的大小为10元素。

Use scanf instead of scanf_s , allocate memory for the char pointer and don't use the & , it is already a pointer. 使用scanf代替scanf_s ,为char指针分配内存,并且不使用& ,它已经是指针。 See example below: 请参见下面的示例:

#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;
}

The scanf family of functions read the data specified by the format string into the variable(s) provided as a parameter(s). scanf系列函数将格式字符串指定的数据读入作为参数提供的变量。

These functions do not allocate storage for a string variable! 这些函数不会为字符串变量分配存储空间! Providing a variable classroom to scanf without having alocated memory for it, will make scanf attempt to place the data at an undefined place in memory. 在没有分配内存的情况下为scanf提供可变的classroom ,将使scanf尝试将数据放置在内存中的未定义位置。 This can result in any type of behavior, generally called undefined behavior . 这可以导致任何类型的行为,通常称为未定义行为

So you must first allocate storage for your string variable, for example: 因此,您必须首先为字符串变量分配存储,例如:

char*classroom= malloc(1024);

an now you can call scanf with it: 现在您可以使用它调用scanf:

scanf_s("%s", classroom);

Note that, because a char pointer behaves as an array (ie it points to an array of char), you don't use the address of operator, & . 请注意,由于char指针的行为就像一个数组(即,它指向char的数组),因此您不必使用运算符&的地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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