Pass array of structs as argument

1

Having the struct

struct bloco {
    bloco () : real(), marcado('.'){}

    char real;
    char marcado;
};

When I try

void tabuleiro (int lado, int mina){
    ...
    bloco tab[lado][lado];

    ...

    for (i=0, j=0; lado>i; i++){
        for (j=0; lado>j; j++){
             tab[i][j].real = '.';
        }
    }

    ...

    jogar(tab[lado][lado]);

    return;
}

And in the play function I have

void jogar(bloco tab){
    tab[x][y].marcado = 'M'; 
}

I get the message

  

no match for 'operator []' (operand types are 'block' and 'int')      tab [x] [y] .marcado = 'M';

As far as I understand, this means that I did not pass "tab" as an array, so I tried to solve with:

void jogar(int lado, bloco tab[lado][lado]){
    tab[x][y].marcado = 'M'; 
}

That gave me the following message

  

error: use of parameter 'side' outside function body    void play (int side, block tab [side] [side]) {

I just wanted to pass the array of structs created in one function to change it in the other, but I do not know how to: (

    
asked by anonymous 03.11.2015 / 00:05

1 answer

0

The second error you received is because you need to pass a constant size inside the brackets in the parameter declaration. "side" does not serve, because it is also a parameter. Even if it were, side is not a constant or constant expression , so it can not be used as a dimension of the array.

That is, you will have to fix the size of your array and, once fixed, do something like this:

void jogar(bloco tab[][ALTURA]) { ... }

Apparently, this does not fit your requirements, so you can use std:vector s, which are of dynamic size. std::vector of std::vector , in your case.

    
03.11.2015 / 16:31