动态内存结构体图书馆
#include<stdio.h>#include<stdlib.h>
main()
{
struct Library {
char name;
char author;
long int date;
};
int i;
structLibrary *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));
printf("please input %d author of book:", m);
scanf_s("%s", &((book + m - 1)->author));
printf("please input %d date of book:", m);
scanf_s("%ld", &((book + m - 1)->date));
if(*((book+m-1)->name)==-1)
{
break;
}
m++;
}
for (i = 0; i < m-1; i++)
{
printf("%s\n", (book + i)->name);
printf("%s\n", (book + i)->author);
printf("%ld\n", (book + i)->date);
}
}
好像写入和打印有问题,求大佬DEBUG 我的编译器不支持scanf_s,你手动改一下
#include<stdio.h>
#include<stdlib.h>
// 结构体以后最好定义在函数外 ,你的也没影响,不过影响 用户阅读
struct Library
{
char name;
char author;
long int date;
};
int main(void)
{
int i;
structLibrary *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",((book+m-1)->name)); // 输入的是字符串,不需要加&
// 判断语句写到这里比较好,避免用户想退出时还要再输两个值
if(strcmp(((book+m-1)->name), "-1") == 0) // 两个字符串比较,而不是字符与整型常量
{
break;
}
printf("please input %d author of book:", m);
scanf("%s", ((book + m - 1)->author)); // 同上
printf("please input %d date of book:", m);
scanf("%ld", &((book + m - 1)->date));
m++;
/* if(((book+m-1)->name)=="-1")
{
break;
}
m++;
*/
}
for (i = 0; i < m-1; i++)
{
printf("%s\n", (book + i)->name);
printf("%s\n", (book + i)->author);
printf("%ld\n", (book + i)->date);
}
} 巴巴鲁 发表于 2020-9-26 22:11
我的编译器不支持scanf_s,你手动改一下
0x0F72E63C (ucrtbased.dll)处(位于 Project2.exe 中)引发的异常: 0xC0000005: 写入位置 0x009DE000 时发生访问冲突。
大佬,现在就是SCANF输入不管加不加&,都可运行,但是写入数据后,会报错 汽车行业工程师 发表于 2020-9-26 23:28
0x0F72E63C (ucrtbased.dll)处(位于 Project2.exe 中)引发的异常: 0xC0000005: 写入位置 0x009DE000 时发 ...
改成scanf_s(),不行了?
再后面添加指定长度scanf_s("%s",((book+m-1)->name),40),同理,后面那个加个10 巴巴鲁 发表于 2020-9-27 08:03
改成scanf_s(),不行了?
再后面添加指定长度scanf_s("%s",((book+m-1)->name),40),同理,后面那个加个1 ...
谢谢大佬,问题就出在VS需要在scanf字符串后标定大小,以及字符串比较调用函数 汽车行业工程师 发表于 2020-10-13 22:53
谢谢大佬,问题就出在VS需要在scanf字符串后标定大小,以及字符串比较调用函数
vs还行吧,最近也是在摸索
以前用的都是dev 巴巴鲁 发表于 2020-9-27 08:03
改成scanf_s(),不行了?
再后面添加指定长度scanf_s("%s",((book+m-1)->name),40),同理,后面那个加个1 ...
麻烦看下,最后改成指定查看的话哪有问题,提示的是lookupSTACK周围已被占据
#include<stdio.h>
#include<stdlib.h>
main()
{
struct Library {
char name;
char author;
long int date;
};
int i;
structLibrary *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;
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);
} 汽车行业工程师 发表于 2020-10-13 23:46
麻烦看下,最后改成指定查看的话哪有问题,提示的是lookupSTACK周围已被占据
查询的结果可以正确运行出来,但是最后大括号处报错Run-Time Check Failure #2 - Stack around the variable 'lookup' was corrupted.。我想lookup就是普通变量,
页:
[1]