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.");
}
Ich schreibe einen Schutz über einige Lotto-Dateien. Ich habe dieses Problem in meiner Funktion, die dem Lotteriefeld mehr Daten hinzufügt. Wann immer x
1989 enthält, gibt mein realloc
Aufruf NULL
zurück. Ich setze x
auf 1985 und i
könnte 4 weitere Eingänge zum Array hinzufügen, aber immer wenn x
1989 ist, gibt es immer noch NULL
zurück. Meine Frage ist: ist etwas mit dem Code falsch oder ich habe immer noch nicht genug Speicher?Realloc Rückgabe NULL
Wir müssen etwas mehr Code sehen; Könntest du ein "main" einfügen, das zeigt, wie diese Funktion aufgerufen wird? – Bathsheba
Sie müssen nicht auf "malloc/realloc" übertragen –
Und bitte formatieren Sie Ihren Code richtig. –