Hi, im trying to finish up my assignment on tic tac toe game.. and my program keeps crashing, here are two functions where i think it crashes, probably the first one:

void tic_tac_toe_init (char *ttt[3][3])
{
    char i,j;
    int k=' ';
    for (i=0;i<3;i++)
    {
        for (j=0; j<3; j++)
            {
            *ttt[i][j]=(char)k;
            printf ("%c\n", *ttt[i][j]);
        }    
    }
} 
is_tic_tac_toe_full (char *ttt[3][3])
{
    int i, j, k=0;
    for (i=0;i<3;i++)
    {
        for (j=0; j<3; j++)
        {
            if ((int)*ttt[i][j]==' ')
            {
                k++;
            }
        }    
    }
    if (k>=1)
    {
        return 1;
    }
    else 
    {
        return 0;
    }
}

and also a second question for another program: When i initialize the array for example a[2][2] and i want to fill it like this:

char a[2][2]={{' ', 1, "11-12PM"},
              {'A', 1,2},
              {"sad", 3,4}} and so on

for some reason it gives me errors
thanks in advance

Recommended Answers

All 5 Replies

heres the error i get for the second question:
2.c:6: warning: initialization makes integer from pointer without a cast. Oh i use linux gcc compiler if that makes any difference.

>>if ((int)*ttt[j]==' ')
1. No need for the typecase.
2. loop couters should be int, not char
3. Remove the * from the array declaration
4. Functions must have return types -- default returns is not permissable.
5. Variable k chould be declared as char not int. int is_tic_tac_toe_full(...

void tic_tac_toe_init (char ttt[3][3])
{
    int i,j;
    char k=' ';
    for (i=0;i<3;i++)
    {
        for (j=0; j<3; j++)
            {

            ttt[i][j]= k;
            printf ("%c\n", ttt[i][j]);
        }
    }
}
int is_tic_tac_toe_full (char ttt[3][3])
{
    int i, j, k=0;
    for (i=0;i<3;i++)
    {
        for (j=0; j<3; j++)
        {
            if (ttt[i][j]==' ')
            {
                k++;
            }
        }
    }
    if (k>=1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

1. I am unable to understand why you have used a declaration like

void tic_tac_toe_init (char *ttt[3][3])

It should only be

void tic_tac_toe_init (char ttt[3][3])

and assignments further below should only be

ttt[i][j]=' ';

2. It's a 2x2 array with capacity to hold maximum of 4 elements.

char a[2][2]={{' ', 1}, {'A', 1}}


2. It's a 2x2 array with capacity to hold maximum of 4 elements.

char a[2][2]={{' ', 1}, {'A', 1}}

sorry, i meant to put a[3][3], it actually way bigger matrix, i just typed really quick a part of it, i didnt want to copy the whole 9x9 matrix.

If you want to use pointers to pass a 2-D array write your code like this

void
printStuff(int* a,int rows,int cols)
{
    int i=0,j=0;
    
    for(i=0;i<rows;i++)
    {
          for(j=0;j<cols;j++)
               printf("%d\t",a[(i*rows) + j]);
    }
}

int
main(int argc, char *argv[])
{
         int a[3][3];
         int i=0,j=0,count=0;
         
         for(i=0;i<3;i++)
         {
               for(j=0;j<3;j++)
                     a[i][j] = count++;
         }
         
         printStuff(&a[0][0],3,3);
                  
         return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.