“program”

This commit is contained in:
Vasylenko 2024-04-19 15:48:31 +02:00
parent 7c4684e788
commit b67900303f

View File

@ -1,4 +1,4 @@
#include <stdio.h> ~~#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -19,13 +19,13 @@ int main() {
names = (char **)malloc(capacity * sizeof(char *)); names = (char **)malloc(capacity * sizeof(char *));
if (names == NULL) { if (names == NULL) {
puts("Memory allocation error"); puts("Memory allocation error");
return 1; return 0;
} }
if (scanf("%d\n", &max_students) != 1 || max_students <= 0) { if (scanf("%d\n", &max_students) != 1 || max_students <= 0) {
puts("Nespravny vstup"); puts("Nespravny vstup");
free(names); free(names);
return 1; return 0;
} }
while (fgets(name, MAX_NAME_LENGTH, stdin) != NULL) { while (fgets(name, MAX_NAME_LENGTH, stdin) != NULL) {
@ -43,17 +43,22 @@ int main() {
if (size == capacity) { if (size == capacity) {
capacity *= 2; capacity *= 2;
names = (char **)realloc(names, capacity * sizeof(char *)); char **new_names = (char **)realloc(names, capacity * sizeof(char *));
if (names == NULL) { if (new_names == NULL) {
puts("Memory allocation error"); puts("Memory allocation error");
return 1; for (int j = 0; j < size; j++) free(names[j]);
free(names);
return 0;
} }
names = new_names;
} }
names[size] = strdup(name); names[size] = strdup(name);
if (names[size] == NULL) { if (names[size] == NULL) {
puts("Memory allocation error"); puts("Memory allocation error");
return 1; for (int j = 0; j < size; j++) free(names[j]);
free(names);
return 0;
} }
size++; size++;
} }
@ -61,7 +66,7 @@ int main() {
if (size == 0) { if (size == 0) {
puts("Ziadne prihlasky"); puts("Ziadne prihlasky");
free(names); free(names);
return 1; return 0;
} }
qsort(names, size, sizeof(char *), compare); qsort(names, size, sizeof(char *), compare);
@ -85,3 +90,5 @@ int main() {
free(names); free(names);
return 0; return 0;
} }