Struct and matrix, how to relate?

Asked

Viewed 255 times

1

I’m doing a code for a college paper and a question has arisen about how to assign values to a matrix of the type struct.

 struct posicao
{
int x;
int y;
};
struct posicao inimigo_val[5][15] = {
{{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1}},
{{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1}},
{{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1}},
{{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1}},
{{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1}},
{{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1},{0,-1}}
};

I thought to do the attribution this way, would that be correct? Is there a problem in the use of struct with matrix? Is there a more practical way to assign values? Remembering that they would be different values in each member, this was just an example.

1 answer

1


Yes, that is correct.

No problem using this, it’s actually quite common.

There are several other ways to declare, but this one looks good. Some would prefer to be explicit using the names of the members of the structure, but it is not fundamental. It is like readability (questionable, because it gets longer and can even harm, everything depends on context).

  • very obg, and in case I want to access the values of this matrix?

  • should use engo_val[i][j]. x? in this way?

  • Exactly that.

Browser other questions tagged

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