Runtime error in C

Asked

Viewed 162 times

0

Well, it’s compiling everything ok, however, when I print out the elements of the entire tab matrix[8][8], which is a member of the checksum structure, the compiler, in a way is assigning numbers to this matrix, which I don’t know where they came from, since the matrix should contain 1, -1 or 0. However, it stores relatively large values, which I suspect are memory positions. I managed to get around this obstacle by passing the address of the main structure to the functions (obviously, the parameters of the functions have also been modified to allow this). Then, in the functions, I used the operator of (->) instead of (.) since I was working with the pointer of a structure. The fruits from this, were the goals that I longed for. However, I did 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");
}
  • 1

    And what should happen? It’s running like this: http://ideone.com/CHrz3X

1 answer

0


The problem is that you are passing a copy of the array to the function inicializar(). 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 function inc_int() is exactly the same with your code: it gets a copy of the variable x and incrementing only affects the local copy. Already the function inc_intptr() receives the address of the variable x declared in function main() and when changed in function, affects the original variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.