LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help needed with segmentation fault tried solving a lot please help (https://www.linuxquestions.org/questions/programming-9/help-needed-with-segmentation-fault-tried-solving-a-lot-please-help-646086/)

kunalghosh 05-31-2008 12:50 PM

help needed with segmentation fault tried solving a lot please help
 
i am getting segmentation fault

in the

****************code*****************

#include <stdio.h>

int ind[1000][1];/*here row or m is 0
and column or n is 1*/


int main()
{
char field[1000][100][100];
int count=-1,i,j,p,q;



while(1)
{
count+=1;
scanf("%d%d",&ind[1][0],&ind[1][1]);

if((ind[count][0]==0)&&(ind[count][1]==0))
{
break;
}

for(i=0;i<ind[count][0];i++)
{
for(j=0;j<ind[count][1];j++)
{
scanf("%c",&field[count][i][j]);
if(field[count][i][j]=='.')
{
field[count][i][j]+=2;
}
}
}

/*processing the output*/

for(i=0;i<ind[count][0];i++)
{
for(j=0;j<ind[count][1];j++)
{
if(field[count][i][j]=='*')
{
continue;
}
else
{
for(p=-1;p<=1;p++)
{
for(q=-1;q<=1;q++)
{
if(((i+p)<0)||((j+q)<0))
{
continue;
}
else if(((i+p)>ind[count][0])||((j+q)>ind[count][1]))
{
continue;
}
else if(field[count][i+p][j+q]=='*')
{
field[count][i][j]+=1;
}
}
}

}
}
}
}
for(i=0;i<=count;i++)
{
printf("Field #%d:",i+1);

for(p=0;p<ind[i][0];p++)
{
for(q=0;q<ind[i][1];q++)
{
printf("%c ",field[i][p][q]);
}

printf("\n");
}
}
return(0);
}
*******************end of code***********************
could somebody please help

Maligree 05-31-2008 12:52 PM

Please, PLEASE use code tags..

Nylex 05-31-2008 12:55 PM

scanf("%d%d",&ind[1][0],&ind[1][1]);

Are you just getting a seg fault on this line because you're trying to access element 1 in the second dimension of your array, which only has one element? Of course, if there's only one element, then the only element you can access is 0.

jim mcnamara 05-31-2008 02:44 PM

Answer:
Code:

change this:
int ind[1000][1];

to this:
int ind[1000][2];

You also should initialize the array: int ind[1000][2]={0};


All times are GMT -5. The time now is 01:35 AM.