i am trying to make an array of strings with 10 chars in each string, and amount of string is unknown , so each time i have a new string , i need to increase the array size by one and insert the string in the new index. the program prints only one string and then segmentation fault. why is that?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void StoreStrings(char ***,int *);
int main()
{
int i;
char **strings = (char**)malloc(sizeof(char*));
int size=0;
for(i=0;i<10;i++)
{
StoreStrings(&strings,&size);
printf("%s\n",strings[size]);
size++;
}
return 0;
}
void StoreStrings(char ***storage,int *size)
{
char *string = NULL;
char **temp= (char**)realloc(*storage,(*size+1) * sizeof(char*));
string="000000000110";
if (temp == NULL)
{
printf("Cannot allocate more memory.\n");
}
else
{
*storage=temp;
*storage[*size] = (char*)malloc(10*sizeof(char));
strcpy(*storage[*size],string);
}
}