LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-31-2008, 12:50 PM   #1
kunalghosh
LQ Newbie
 
Registered: Mar 2007
Distribution: suse linux 9.1,xubuntu
Posts: 29

Rep: Reputation: 15
Unhappy 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
 
Old 05-31-2008, 12:52 PM   #2
Maligree
Member
 
Registered: Mar 2008
Distribution: Gentoo, CentOS, Fedora, Arch
Posts: 231
Blog Entries: 1

Rep: Reputation: 42
Please, PLEASE use code tags..
 
Old 05-31-2008, 12:55 PM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.
 
Old 05-31-2008, 02:44 PM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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};
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed in segmentation fault babycorn Programming 3 02-15-2006 12:43 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Segmentation fault Varadharajan Programming 5 04-22-2005 10:26 AM
C Segmentation Fault fatman Programming 20 04-02-2003 05:16 PM
segmentation fault sqn Slackware 3 03-21-2003 05:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration