I would like to fill an array of strings using two functions: the first, if I have n strings to allocate, will allocate n memory spaces; the second will allocate memory for each string read
Here is the first function:
char** allocate(int n)
{
char** t;
t=(char**)malloc(n*sizeof(char*));
if(!t) exit(-1);
return t;
}
Here is the second one:
void fill(char*** t,int n)
{
int i;
char* help=" ";
for(i=0;i<n;i++)
{
printf("\n saisir la chaine n %d :",i+1);
scanf("%s",help);
*t[i]=(char*)malloc((strlen(help)+1)*sizeof(char));
strcpy(*t[i],help);
}
}
I did not forget to call the second one in main like this : fill(&t,n);
The problem is that I get an error after reading the first string and program ends.
char* help=" ";scanf("%s", help)thenhelpmust point to a memory location large enough to hold the string that is being read in.mallocin C.