From 055d78227529d3c7f6780457d9db8cfddc27e2e1 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Sat, 6 Jun 2020 17:09:51 -0400 Subject: [PATCH] use dynamic memory --- exercism/acronym/acronym.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/exercism/acronym/acronym.c b/exercism/acronym/acronym.c index 3ddfaaf0..4c0477f3 100644 --- a/exercism/acronym/acronym.c +++ b/exercism/acronym/acronym.c @@ -38,11 +38,12 @@ char *abbreviate(const char *phrase) i = 0; counter++; - char words[counter][80]; + char **words = (char **)malloc(counter * sizeof(char *)); /* initalizes words-array with empty strings */ for (i = 0; i < counter; i++) { + words[i] = (char *)malloc(80 * sizeof(char)); strcpy(words[i], ""); } @@ -83,5 +84,9 @@ char *abbreviate(const char *phrase) strcat(acr, words[i]); } + for (i = 0; i < counter; i++) + free(words[i]); + free(words); + return acr; -} \ No newline at end of file +}