#include <stdio.h>
#include <stdlib.h>
#include <conio.h> // _kbhit() _getch
#include <string.h>
typedef struct
{
int id;
char name[20];
int age;
} student;
int main()
{
student *p = malloc(0 * sizeof(student));
if(p==NULL){
perror("分配内存失败!");
return -1;
}
int index = 1;
printf("请录入学员信息,以 ESC 结束录入!\n");
while (1)
{
if (index != 1)
{
printf("按任意键继续录入\n");
}
if (index > 1)
{
if (_getch() == 27) // Esc ASCLL码
{
printf("录入完成\n");
break;
}
}
printf("第%d位学生\n", index);
p = realloc(p, index * sizeof(student));
p[index - 1].id = index;
printf("名字:");
scanf("%s", &p[index - 1].name);
printf("年龄:");
scanf("%d", &p[index - 1].age);
index++;
}
for (int i = 0; i < index - 1; i++)
{
printf("ID=%d\t", p[i].id);
printf("名字=%s\t", p[i].name);
printf("年龄=%d\t", p[i].age);
printf("\n");
}
free(p);
return 0;
}
// 如何获取自己键盘上按键的键值(KeyCode): https://blog.51cto.com/u_15055361/5537015