数组访问越界问题求解
程序编译正确,运行过程中出现错误:DAMAGE:after Normal block (#45) at 0x003707AB#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct
{
int hang;//行标
int lie;//列标
int zhi;//值
}HLZ;
typedef struct
{
HLZ * sz;
int hs;//矩阵的行数
int ls;//矩阵的列数
int fls;//矩阵非零个数
}SHLF;
int main()
{
HLZ * sl;
int a=0,b=0,i,j;
int ar[12][12]=
{
{0,0,2,0,0,0,0,0,0,7,0,0},
{0,0,0,0,0,0,0,0,0,8,0,0},
{0,0,0,0,0,0,0,0,0,8,0,0},
{0,0,0,0,0,0,7,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,9,0,0},
{0,0,0,0,0,0,0,0,0,8,0,0},
{0,0,0,0,0,0,5,0,0,0,0,0},
{0,0,0,0,0,6,0,0,0,0,0,0},
{0,0,0,4,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,8,0,0},
{0,0,0,0,7,0,0,0,0,0,0,0},
};
for(i=0;i<12;i++)//找稀疏矩阵非零元素
{
for(j=0;j<12;j++)
{
if(ar[i][j]!=0)
a++;
}
}
printf("a=%d\n",a);
sl=(HLZ *)calloc(a,sizeof(int));
for(i=0;i<12;i++)
{
for(j=0;j<12;j++)
{
if(ar[i][j]!=0)
{
sl[b].hang=i;
sl[b].lie=j;
sl[b].zhi=ar[i][j];
b++;
}
}
}
for(i=0;i<a;i++)
printf("第%d行,第%d列,值是: %d \n",sl[i].hang,sl[i].lie,sl[i].zhi);
// 到这里就错了,自己找过知道是数组访问越界,但不会改。求高人指点。
free(sl);
sl=NULL;
getch();
return 0;
}