-2
我想每次输入一个字符到每个块每行,但是会发生什么是最后一行将覆盖我存储在前一行中的以前的内容。最后,我所有的行都有相同的内容..有人可以解释我做错了什么吗?为什么我的双指针会覆盖它的行?
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
int row=0,col=0,i;
char c;
char **this=NULL;
this=calloc(64,sizeof(char*));
for(i=0;i<64;++i)
{
this[i]=calloc(5,sizeof(char));
free(this[i]);
}
while(c!=EOF)
{
c=getchar();
if(!isspace(c)&&isprint(c))
{
if(c==',')
{
this[row][col]='\0';
row++;
col=0;
}
else if(c=='.')
{
this[row][col]=c;
this[row][col+1]='\0';
}
else
{
this[row][col]=c;
//printf("%d,%d\n",row, col);
//printf("%c\n",this[row][col]);
//printf("%s\n",this[row]);
//printf("%s\n",this[row+1]);
col++;
}
}
}
printf("string0:%s\n",this[0]);//prints the same thing
printf("string1:%s\n",this[1]);
free(this);
return 0;
}
分配之后,“free(this [i])'是什么意思?您之后对任何指针的使用都是未定义的行为。 –
我知道,每当我分配,我需要释放它,所以我仍然可以重用的空间?但我真的不知道我应该在哪里释放它。 –
当你完成它的时候,你释放内存。如果您尝试在释放后使用分配的内存(以及指针对象),则这是未定义的行为。 –