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