我无法在代码中找到无法正常工作的地方。我每次都会遇到细分错误。 我需要使用静态缓冲区来写随机长度的“ M”行而没有分隔符。然后,我需要将“ M”的地址写入动态地址“ A”的数组。 问题出在输入功能上。 “ N”是256。
int input()
{
int c;
while((c=getchar())!='\n')
{
if(c=='\t' || c==' ')
{
}
else
{
return c;
}
}
return '\0';
}
int buffer(int buf[N])
{
int c,i=0;
for(i=0;i<N && (c=input())!='\0';i++)
{
if(c!=EOF)
buf[i]=c;
else
return EOF;
}
buf[N]='\0';
return 0;
}
int size(register const int *s)
{
register int *p;
p=s;
while(*p!='\0')
p++;
return p-s;
}
int main()
{
int** A;
int* M;
int n,i=0,j=0;
int buf[N];
while(buffer(buf)!=EOF)
{
if(i%20==0)
if((A=(int**)malloc(20*sizeof(int)))==NULL)
{
printf("Error! Memory not allocated.");
exit(0);
}
if((M=(int*)malloc(size(buf)+1))==NULL)
{
printf("Error! Memory not allocated.");
exit(0);
}
j=0;
while(buf[j]!='\0')
{
*M=buf[j];
M++;
j++;
}
*M='\0';
**A=*M;
i++;
}
free(M);
free(A);
return 0;
}