|
楼主 |
发表于 2020-10-13 23:46:46
|
显示全部楼层
麻烦看下,最后改成指定查看的话哪有问题,提示的是lookup STACK周围已被占据
#include<stdio.h>
#include<stdlib.h>
main()
{
struct Library {
char name[30];
char author[10];
long int date;
};
int i;
struct Library *book = NULL;
int m=1;
while (1)
{
book= (struct Library*)realloc(book, m * sizeof(struct Library));
printf("please input %d name of book(iput -1 exit):", m);
scanf_s("%s",((book+m-1)->name),31);
if (strcmp(((book + m - 1)->name), "-1") == 0) // 两个字符串比较,而不是字符与整型常量
{
break;
}
printf("please input %d author of book:", m);
scanf_s("%s", ((book + m - 1)->author),11);
printf("please input %d date of book:", m);
scanf_s("%ld", &((book + m - 1)->date),8);
m++;
}
char lookup[30];
int k=0;
printf("please input name of book wiht you want to lookup:");
scanf_s("%s",&lookup, 31);
for (i = 0; i < m-1; i++)
{
if (strcmp(((book + i)->name),lookup) == 0) // 两个字符串比较,而不是字符与整型常量
{
break;
}
k++;
}
printf("%s\n", (book + k)->name);
printf("%s\n", (book + k)->author);
printf("%ld\n", (book + k)->date);
} |
|