Please go to line 29 and read the comment.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char* messenges[5];
char* allstrings;
register int i;
int total = 0;
for (i = 0; i < 5; i++)
{
printf("Enter a string: ");
messenges[i] = (char*)malloc(5 * sizeof(char));
scanf("%s", messenges[i]);
total += strlen(messenges[i]);
}
allstrings = (char*)malloc(total);
strcpy(allstrings, messenges[0]);
for (i = 1; i < 5; i++)
{
strcat(allstrings, " ");
strcat(allstrings, messenges[i]);
}
printf("%s", allstrings);
free(allstrings); // This does not work..WHY?(Visual Studio 2015)
for (i = 0; i < 5; i++)
{
free(messenges[i]);// This works fine.
}
getch();
return 0;
}
Above code doesn't free allstrings but it can free messenges[i]. Why doesn't it work for allstrings as well?