NO

Author Topic: SEGMENTATION FAULT  (Read 2243 times)

cooljaini

  • Guest
SEGMENTATION FAULT
« on: December 22, 2011, 06:58:03 AM »
 // i m gettin segmantation fault in my c code for tic tac toe game.. can ny one help me ..


//Two player game of tic tac toe

#include<stdio.h>

int a[2][2]={0,0,0,0,0,0,0,0,0};
int pos1,pos2,flag=0,max=0;
void fill(int flag)
{
    int l,m,n;
    while(max<=9)
    {
          if(flag%2==0)
          {
               B:
               printf("Player 1 enter pos1 and pos2 varying between 0 to 2\n");
               scanf("%d %d",pos1,pos2);
               
               if(a[pos1][pos2]==4)
               {
                   printf("Entry is invalid/already occupied\n");
                   goto B;
               }
               else
               {
                  a[pos1][pos2]=3;
                  flag++;
                  max++;
                  l=check_row(pos1,pos2);
                  m=check_column(pos1,pos2);
                  if(l==1 || m==1)
                  {result(flag);}
                  if((pos1-pos2)%2==0)
                  {
                  n=check_diagonal(pos1,pos2);
                  if(n==1)
                  {result(flag);}
                  }
                   }
          }
          else
          {
               A:
               printf("Player 2 enter pos1 and pos2 varying between 0 to 2\n");
               scanf("%d %d",pos1,pos2);
               
               if(a[pos1][pos2]==3)
               {
                   printf("Entry is invalid/already occupied\n");
                   goto A;
               }
               else
                  {
                     a[pos1][pos2]=4;
                     flag++;
                     max++;
                     check_row(pos1,pos2);
                     check_column(pos1,pos2);
                      if(l==1 || m==1)
                     {result(flag);}

                     if((pos1-pos2)%2==0)
                     {
                     check_diagonal(pos1,pos2);
                        if(n==1)
                     {result(flag);}
                     }

                  }
          }
    }
}


int check_diagonal(pos1,pos2)
{
    int i,count=0;
    if(pos1==pos2)
    {
        for(i=0;i<2;i++)
        {
            if(a==a[i+1][i+1])
            {
                count++;
            }
        }
           if(count==2)
            {
                return 1;
            }

    }
    else
    {
        i=1;
        if(a==a[i+1][i-1]==a[i-1][i+1])
        {
         return 1;
        }

    }


}

int check_row(int pos1,int pos2)
{
    if(a[pos1][0]==a[pos1][1]==a[pos1][2])
    {return 1;}
}

int check_column(int pos1,int pos2)
{
if(a[0][pos2]==a[1][pos2]==a[2][pos2])
    {return 1;}
}

result(int flag)
{
    int k;
    if(flag%2==0)
    {
        k=1;
        }
        else
        {
            k=2;
            }
    printf("Game over player %d wins,k");

}


void main()
{
    int i;
    printf("Game starts with player1\n");
    fill(flag);

}

CommonTater

  • Guest
Re: SEGMENTATION FAULT
« Reply #1 on: December 22, 2011, 08:36:42 AM »
First... please use code tags around your source code...

Also go into...
Project -> Project Options -> Compiler -> Warnings = Level 2

There's lots in your code the compiler should be warning you about, and you should pay attention. 
For example...
Code: [Select]
int a[2][2]={0,0,0,0,0,0,0,0,0};  Should be getting you a warning about too many initializers. 

Global variables should be avoided whenever possible.  You will sometimes need them (eg. for settings) but whenever you can avoid them you should.

Making the whole game into a function does nothing better than wasting main() ... most of what you have in your fill function could probably be moved into main without any consequences at all.

Also try to use more descriptive variable names... k and a and f just don't do it... give them real names like board and square and row and column ... so that their names indicate their use.  You will find debugging much easier.

The segfault itself is most likely the result of you consistently going beyond array bounds.  You have dimentioned your array as 2 x 2 ... that gives you valid indexes of 0 and 1 for each dimension, yet you allow the player to go beyond that.  C arrays always start at 0 ... so if you want indexes of  0, 1 and 2 ... you need an array of 3 x 3.

Don't use Goto!  It may be useful for debugging code or handling some obscure errors, but it's not a good idea for general use.  Rearrange your code to avoid it.

Hope this helps...