Run-time error in C [closed]

0

Well, it's compiling everything ok, however, when I'm going to print the elements of the entire array tab [8] [8], which is a member of the lady structure, the compiler is somehow assigning numbers to this array, the which I do not know where they came from, since the array should contain 1, -1 or 0. However, it stores relatively large values, which I suspect are memory locations. I managed to circumvent this constraint by passing the address of the lady structure to the functions (obviously, the parameters of the functions have also been modified, so as to allow this). So, in the functions, I used the (-> gt;) operator instead of (.), Since I was working with the pointer of a structure. The fruits from this were the goals I wanted. However, I do not understand why the code below does not work as it should.

#include<stdio.h>

#define SIZE 8

typedef struct jogo {
    int tab[SIZE][SIZE];
    /*p[0] == constante que denota 
    o player enquanto p[1] == qt de 
    pecas que o jogador possui*/
    int player_1[2];
    int player_2[2];
} tabuleiro;

void inicializar (tabuleiro p);
void mostrar (tabuleiro p);

int main() {
    register int i, j;
    tabuleiro dama;
    inicializar(dama);
    mostrar(dama);
    return 0;
}

/*Inicializa o tabuleiro de dama*/
void inicializar (tabuleiro p) {
    register int i, j;

    p.player_1[0] = -1;
    p.player_2[0] = 1;
    p.player_1[1] = p.player_2[1] = SIZE;

    for (i = 0; i < SIZE; ++i) {
        for (j = 0; j < SIZE; ++j) {
            if ((i == 0 && j % 2 == 1) 
            || (i == 1 && j % 2 == 0))
                p.tab[i][j] = p.player_1[0];

            else if ((i == 6 && j % 2 == 1) 
            || (i == 7 && j % 2 == 0))
                p.tab[i][j] = p.player_2[0];

            else p.tab[i][j] = 0;
        }
    }
}

/*Apresenta o tabuleiro de dama*/
void mostrar (tabuleiro p) {
    register int i, j;

    printf("\n\n|---|---|---|---|---|---|---|---|\n");
    for (i = 0; i < SIZE; ++i) {
        for (j = 0; j < SIZE; ++j) {
            if (p.tab[i][j] == p.player_1[0])
                printf("| X ");

            else if (p.tab[i][j] == p.player_2[0])
                printf("| O ");

            else
                printf("|   ");
        }
        printf("|\n|---|---|---|---|---|---|---|---|\n");
    }
    printf("\n\n");
}
    
asked by anonymous 29.01.2017 / 08:38

1 answer

0

The problem is that you are passing a copy of the array to the inicializar() function. The copy is modified correctly within the function, but the original variable remains with the original values, which are random values that were in the statically allocated memory area.

For a better understanding, see this example using a simpler type:

void inc_int(int x) {
  x++;
}

void inc_intptr(int *x) {
  (*x)++;
}

int main() {
  int x = 0;

  inc_int(x);
  printf("x = %d\n", x); // imprimirá "x = 0, pois a somente a cópia será incrementada"
  inc_intptr(&x);
  printf("x = %d\n", x); // imprimirá "x = 1"

  return 0;
}

The problem with the inc_int() function is exactly the same with your code: it receives a copy of the variable x and the incrementation only affects the local copy. The inc_intptr() function gets the address of the x variable declared in the main() function and when changed in the function, it affects the original variable.

    
31.01.2017 / 13:18