int main() {
struct lottery *array;
array = (struct lottery *)malloc(3000 * sizeof(struct lottery));
int opt, counter;
menu1();
scanf("%d", &opt);
if (opt == 1)
Load(array, &counter);
else
exit("0");
menu2();
counter--;
scanf("%d", &opt);
while (opt != 7) {
switch (opt) {
case 1:
Save(array);
break;
case 2:
Enterd(array, &counter);
printf("%d\n", counter);
break;
}
menu2();
scanf("%d", &opt);
}
return 0;
}
void Enterd(struct lottery *a, int *count) {
struct lottery *b;
int x;
(*count)++;
x = *count;
printf("Your new data will have an ID of %d\n",x);
a[x].aa = x;
b = (struct lottery *)realloc(a, x * sizeof(struct lottery));
if (b == NULL) {
printf("Memory could not be allocated for your new input.Program will now exit...\n");
exit("0");
}
a = b;
printf("What is the date of your new draw?\n");
scanf("%d/%d/%d", &a[x].date1.day, &a[x].date1.month, &a[x].date1.year);
printf("Now please insert the 5 non-joker numbers\n");
scanf("%d%d%d%d%d", &a[x].n1, &a[x].n2, &a[x].n3, &a[x].n4, &a[x].n5);
printf("What is the 'Joker' number of this draw?\n");
scanf("%d", &a[x].joker);
printf("Your input is now complete.");
}
我正在撰写一份有关彩票文件的保护报告。在我的函数中,我遇到了一个问题,即向彩票数组添加更多数据。每当x
包含1989时,我的realloc
调用就会返回NULL
。我将x
设置为1985,i
可以向数组中添加4个以上的输入,但是当x
为1989时,它仍然返回NULL
。我的问题是:代码是否有问题,还是我的内存仍然不足?
main
函数的代码,展示如何调用这个函数。 - Bathshebamalloc/realloc
上进行强制类型转换。 - Ed Healstruct lottery
的定义在哪里? - dbush