I am having some trouble understanding how memory is allocated to sir[i]
. From what I understand (char*)malloc(sizeof(char))
should only allocate space for one character, but when I run the code it can read any word, regardless of length. Can someone plese explain how/why does this happen?
void read(char **sir,int **ln,int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Read word %d: ",i+1);
sir[i]=(char*)malloc(sizeof(char));
fflush(stdin);
scanf("%s",sir[i]);
ln[i]=(int*)malloc(sizeof(int));
*(ln[i])=strlen(sir[i]);
}
}