求教一个NULL pointer assignment的问题
代码在下面,所有指针在使用前都已经分配了地址,程序运行结果正确,为什么运行后总有NULL pointer assignment提示?
#include<stdio.h>
typedef int * array;
typedef struct node VNode;
typedef VNode *PNode;
struct node
{
int i;
int j;
int type;
PNode pre;
PNode nex;
};
PNode head=NULL,tail=NULL;
int ability;
int row,col;
array * matrix=NULL;
void error(char *s)
{
printf(s);
getch();
exit(1);
}
void insert_virus(int type, int i, int j)
{
PNode tmp=NULL,ptr=NULL;
tmp=(PNode)malloc(sizeof(VNode));
if(tmp==NULL)
error("Memory allocate error");
tmp->i=i;
tmp->j=j;
tmp->type=type;
tmp->pre=NULL;
tmp->nex=NULL;
if(tail==NULL)
{
tail=tmp;
head=tail;
}
else if(type>=(tail->type))
{
tail->nex=tmp;
tmp->pre=tail;
tail=tmp;
}
else
{
ptr=tail;
while(ptr!=NULL&&(ptr->type)>type)
ptr=ptr->pre;
if(ptr!=NULL)
{
tmp->nex=ptr->nex;
tmp->pre=ptr;
ptr->nex=tmp;
tmp->nex->pre=tmp;
}
else
{
tmp->nex=head;
head->pre=tmp;
head=tmp;
}
}
}
void delete_virus(PNode virus)
{
if(virus==head)
{
head=head->nex;
head->pre=NULL;
}
else if(virus==tail)
{
tail=tail->pre;
tail->nex=NULL;
}
else {
virus->pre->nex=virus->nex;
virus->nex->pre=virus->pre;
}
free(virus);
}
int check_virus(PNode virus)
{
int flag=0;
int i=virus->i, j=virus->j, type=virus->type;
if(i-1>=0&&matrix[i-1][j]<0)
if(-matrix[i-1][j]>ability)
flag=1;
else
{
matrix[i-1][j]=type;
insert_virus(type,i-1,j);
}
if(i+1<row&&matrix[i+1][j]<0)
if(-matrix[i+1][j]>ability)
flag=1;
else
{
matrix[i+1][j]=type;
insert_virus(type,i+1,j);
}
if(j-1>=0&&matrix[i][j-1]<0)
if(-matrix[i][j-1]>ability)
flag=1;
else
{
matrix[i][j-1]=type;
insert_virus(type,i,j-1);
}
if(j+1<col&&matrix[i][j+1]<0)
if(-matrix[i][j+1]>ability)
flag=1;
else
{
matrix[i][j+1]=type;
insert_virus(type,i,j+1);
}
return flag;
}
int main(void)
{
int i,j;
int casenum,result;
int *virustype=NULL;
PNode ptr=NULL,ptr1=NULL;
casenum=0;
ability=0;
while(1)
{
scanf("%d %d",&row,&col);
if(row==0&&col==0)
break;
casenum++;
matrix=(array *)malloc(row*sizeof(array));
if(matrix==NULL)
error("Memory allocate error");
for(i=0;i<row;i++)
{
matrix[i]=(int *)malloc(col*sizeof(int));
if(matrix[i]==NULL)
error("Memory allocate error");
for(j=0;j<col;j++)
scanf("%d",&matrix[i][j]);
}
for(i=0;i<row;i++)
for(j=0;j<col;j++)
if(matrix[i][j]>0)
insert_virus(matrix[i][j], i, j);
virustype=(int *)malloc((tail->type+1)*sizeof(int));
if(virustype==NULL)
error("Memory allocate error");
for(i=0;i<=tail->type;i++)
virustype[i]=0;
while(head!=NULL)
{
ptr=head;
ability++;
while(ptr!=NULL)
{
i=check_virus(ptr);
ptr1=ptr->nex;
if(i==0)
delete_virus(ptr);
ptr=ptr1;
}
}
for(i=0;i<row;i++)
for(j=0;j<col;j++)
virustype[matrix[i][j]]++;
scanf("%d",&result);
printf("Scenario \#%d\n",casenum);
for(i=0;i<result;i++)
{
scanf("%d",&j);
printf("%d\n",virustype[j]);
}
printf("\n");
free(virustype);
for(i=0;i<row;i++)
free(matrix[i]);
free(matrix);
}
return 0;
}
[解决办法]
亲娘咧..看到这么长就被吓住了...结果还被解决了..LZ不能这么吓唬人啊..
[解决办法]
恭喜,接分
[解决办法]
路过的,学习下
[解决办法]
恭喜。。恭喜。。接分
[解决办法]