From 7cc02efe4e614e1a435513a08b84347a67ef3d49 Mon Sep 17 00:00:00 2001 From: Weber Date: Thu, 21 Mar 2024 21:14:45 +0000 Subject: [PATCH] skuska --- cv5/program.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cv5/program.c b/cv5/program.c index 3adad93..c5805e3 100644 --- a/cv5/program.c +++ b/cv5/program.c @@ -28,27 +28,27 @@ int main() { char line[1024]; while (fgets(line, sizeof(line), stdin)) { int vote_count; - char *name; - sscanf(line, "%d %ms", &vote_count, &name); + char name[1024]; // Use array instead of dynamically allocated memory + sscanf(line, "%d %[^\n]", &vote_count, name); // Read name until newline character if (student_count >= students_capacity) { students_capacity = (students_capacity + 1) * 2; student_t *new_students = realloc(students, students_capacity * sizeof(student_t)); if (new_students == NULL) { - fprintf(stderr, "Chyba"); + fprintf(stderr, "Error: Memory reallocation failed\n"); return 1; } students = new_students; } - students[student_count].name = name; + students[student_count].name = strdup(name); // Duplicate the name students[student_count].vote_count = vote_count; student_count++; } qsort(students, student_count, sizeof(student_t), compare_students); - printf("Results:\n"); + printf("Vysledky:\n"); for (int i = 0; i < student_count; i++) { printf("%d %s\n", students[i].vote_count, students[i].name); free(students[i].name); @@ -56,4 +56,4 @@ int main() { free(students); return 0; -} +} \ No newline at end of file