#include<stdio.h>
#include<stdlib.h>
struct stu
{
char name[5];
int mark;
};
int cmp(const void *q, const void *p)
{
return ((struct stu*)q)->mark - ((struct stu*)p)->mark;
}
int main()
{
struct stu file[5];
for (int i=0;i<5;i++)
{
scanf_s("%s %d", file[i].name, &file[i].mark);//在devc++
//中按回车仍可继续输入,而在vs2019中按回车就直接结束程序了。
}
qsort(file, 5, sizeof(struct stu), cmp);
for (int i = 0;i < 5;i++)
{
printf("%s %d", file[i].name,file[i].mark);
}
}
希望输入的格式是:
name1‘空格’mark1‘\n’
name2‘空格’mark2‘\n’
name3‘空格’mark3‘\n’
name4‘空格’mark4‘\n’
name5‘空格’mark5‘\n’
Warning C4477 'scanf_s' : format string '%s' requires an argument of type 'unsigned int', but variadic argument 2 has type 'int *'
Warning C4473 'scanf_s' : not enough arguments passed for format string
scanf_s("%s %d", file[i].name, &file[i].mark); // 这里少了buffer 设置
你用这个,还有 这 5 太短了,打长了就会出错。
printf("[%d] -- Plese enter the name and mark: ", i);
scanf_s("%s %d", file[i].name, 5, &file[i].mark);
printf("You have enter: %s %d\n\n", file[i].name, file[i].mark);
那 VS2019 应该是用 VC++ MSVC compiler. 微软是反对用 scanf 的,因为 memory leak 问题。