1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-10-29 22:17:23 +01:00

fix(solutions): char* issues with memory allocation

This commit is contained in:
Théo LUDWIG 2023-07-02 17:37:35 +02:00
parent 9c5c2ee48b
commit e2300b117f
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B

View File

@ -8,7 +8,7 @@
int main() { int main() {
char* string = input(); char* string = input();
size_t string_length = strlen(string); size_t string_length = strlen(string);
char* result = malloc(sizeof(char*) * (string_length + 1)); char* result = malloc(sizeof(char) * (string_length + 1));
for (size_t index = 0; index < string_length; index++) { for (size_t index = 0; index < string_length; index++) {
unsigned char number_of_appearances = 0; unsigned char number_of_appearances = 0;
char value_to_search = string[index]; char value_to_search = string[index];
@ -17,7 +17,7 @@ int main() {
number_of_appearances++; number_of_appearances++;
iteration++; iteration++;
} }
char* number_of_appearances_string = malloc(sizeof(char*) * (string_length + 1)); char* number_of_appearances_string = malloc(sizeof(char) * (string_length + 1));
snprintf(number_of_appearances_string, sizeof(result), "%hhu", number_of_appearances); snprintf(number_of_appearances_string, sizeof(result), "%hhu", number_of_appearances);
character_append_many(result, number_of_appearances_string); character_append_many(result, number_of_appearances_string);
character_append(result, value_to_search); character_append(result, value_to_search);