Xiaobai puzzles the question-scanf_s?

this is always the case when it comes to self-learning what is wrong with the structure.

C
Mar.04,2021

first of all, put your code into a code, and use this site's own editor / markdown format to format it, cut it, and never replace it.

as for your code, there are two questions:

  1. scanf_s is a private account of msvc . It requires you to give length information, but you don't.
  2. scanf_s/scanf accepts const char * , and here your name is of type char [4] , that is, it is a group of characters, do not equate the number of characters with fingers, it is of different types ( derived type ). However, the number group can sometimes be converted into a reference, for example, in scanf_s/scanf here, the required form (parameter) refers to the number, right, but the actual (argument) you give is the number, and a transformation occurs when the parameter is transmitted, and the parameter group is read to the reference. If you pass in & name , then you need to understand that the values of name and & name are certainly the same (both are binary numbers, which can be understood as addresses), but the type of category is different. The type of name is listed above, which is char [4] , and which can be converted to reference. However, the type of & name is actually char (*) [4] . It is an indicator pointing to the number group (whose length is 4) and cannot be transferred to const char * . Which is the type required by scanf/scanf_s . So it triggers a routine.

solution:

scanf_s("%s", stu.name, sizeof(stu.name));

traditional version:

first of all, change your code picture into code, and format it into code format with our own editor / markdown syntax. Remember, never post code pictures.

as for your code, there are two problems:

  1. scanf_s is a contraband of msvc . It requires you to give length information, but you don't.
  2. scanf_s/scanf accepts const char * , and here your name is of type char [4] , that is, it is a character array, do not equate the character array with the pointer, they are two different types ( derived type ). However, arrays can sometimes be implicitly converted to pointers, for example, in scanf_s/scanf , the required parameter (parameter) is a pointer, right, but the argument (argument) you give is an array, and an implicit conversion occurs when the parameter is passed, from the array brick to the pointer. If you pass in & name , then you need to understand that although the values of name and & name are the same (both are binary numbers and can be understood as addresses), their types are different. As mentioned above, the type of name is char [4] , and which can be implicitly converted into pointers. However, the type of & name is actually char (*) [4] , which is a pointer to an array (whose length is 4). Cannot be implicitly converted to const char * , which is the type required by scanf/scanf_s . So an exception was thrown.

solution:

scanf_s("%s", stu.name, sizeof(stu.name));
Menu