From b67900303fde466f23f89c4063341969fc356726 Mon Sep 17 00:00:00 2001 From: Vasylenko Date: Fri, 19 Apr 2024 15:48:31 +0200 Subject: [PATCH] =?UTF-8?q?=E2=80=9Cprogram=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cv10/program.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/cv10/program.c b/cv10/program.c index f81a7a1..a3c994a 100644 --- a/cv10/program.c +++ b/cv10/program.c @@ -1,4 +1,4 @@ -#include +~~#include #include #include @@ -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; } + +