Can you please explain the reason of the garbage output instead of the each words in the string..
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int count = 0;
//char** memptr = NULL;
char** push(char* word, char** memptr){
if(count > 1)
memptr = (char**)realloc(memptr, (count * sizeof(char*)) );
memptr[count - 1] = (char* )malloc(strlen(word));
strcpy(memptr[count - 1], word);
return memptr;
}
int main(){
char string[] = "World has changed your thoughts";
int k = 0, j = 0;
char temp[strlen(string)];
char** memptr = (char**)malloc(sizeof(char *));
while(*(string + k)){
printf("%s", temp);
if(*(string + k) != ' ' )
temp[j++] = *(string + k);
else{
temp[j] = '\0';
count++;
memptr = push(temp, memptr);
j = 0;
}
k++;
}
for(k = 0; k < count; k++)
printf("%s\n", memptr[k]);
return 0;
}