use dynamic memory

This commit is contained in:
Krishna Vedala 2020-06-06 17:09:51 -04:00
parent 5360573994
commit 055d782275
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7

View File

@ -38,11 +38,12 @@ char *abbreviate(const char *phrase)
i = 0; i = 0;
counter++; counter++;
char words[counter][80]; char **words = (char **)malloc(counter * sizeof(char *));
/* initalizes words-array with empty strings */ /* initalizes words-array with empty strings */
for (i = 0; i < counter; i++) for (i = 0; i < counter; i++)
{ {
words[i] = (char *)malloc(80 * sizeof(char));
strcpy(words[i], ""); strcpy(words[i], "");
} }
@ -83,5 +84,9 @@ char *abbreviate(const char *phrase)
strcat(acr, words[i]); strcat(acr, words[i]);
} }
for (i = 0; i < counter; i++)
free(words[i]);
free(words);
return acr; return acr;
} }