/ / Pasando array de estructura bidimensional tipificado como argumento de función [cerrado] - c, matrices, struct, arduino, typedef

Pasar una matriz bidimensional de estructuras como argumento de función [cerrado] - c, matrices, estructura, arduino, typedef

TL; DR; El problema fue que olvidé escribir un prototipo para una función. Añadiendo esto se solucionó el problema:

void tt_drawFigure(tt_figure figure);

Texto completo:

Escribo un clon de tetris para Arduino. En mi implementación, tengo un struct representando un punto en pantalla:

struct tt_point
{
int x;
int y;
};

Cada figura es una matriz de su rotación "snaphots" o "frames". Cada cuadro es una matriz de tt_points, que hace de una figura un conjunto bidimensional de tt_points (Puede que te resulte feo, pero la pregunta no es sobre el diseño).

Me gustaría escribir por defecto esta matriz bidimensional y pasarla a una función. Aquí está el typedef que tengo:

typedef tt_point tt_figure[4][4];

Y una figura en "T":

tt_figure tt_T = {
{{0,0}, {0,1}, {0,2},
{1,1}},

{{0,1},
{1,0}, {1,1},
{2,1}},

{{0,1},
{1,0}, {1,1}, {1,2}},

{{0,0},
{1,0}, {1,1},
{2,0}}
};

Los problemas comienzan cuando trato de pasar una figura a una función:

void tt_drawFigure(tt_figure figure) { ... }

El error es:

Tetris:20: error: variable or field "tt_drawFigure" declared void
Tetris:20: error: "tt_figure" was not declared in this scope

¿Cómo debo reescribir la declaración para pasar un tt_figure a una función?

PD Lo hice funcionando declarando una figura como void* y luego lanzar a 4x4 matriz:

void tt_drawFigure(void* figure)
{
tt_point * fig = ((tt_point(*)[4]) figure)[frame_index];
...
}

Pero debería haber una manera más agradable.

ACTUALIZAR. El código que puedes copiar, pegar y tratar de compilar:

struct tt_point
{
int x;
int y;
};

typedef tt_point tt_figure[4][4];

tt_figure tt_T = {
{{0,0}, {0,1}, {0,2},
{1,1}},

{{0,1},
{1,0}, {1,1},
{2,1}},

{{0,1},
{1,0}, {1,1}, {1,2}},

{{0,0},
{1,0}, {1,1},
{2,0}}
};


void setup()
{

}


void loop()
{

}

void tt_drawFigure(tt_figure figure)
{

}

Respuestas

1 para la respuesta № 1

Tu código se compila si cambias:

typedef tt_point tt_figure[4][4]; a

typedef struct tt_point tt_figure[4][4];.

Por ejemplo, este código a continuación compila y funciona bien.

#include<stdio.h>
struct tt_point
{
int x;
int y;
};

/* typedef tt_point tt_figure[4][4]; /*  /* Issue here */
typedef struct tt_point tt_figure[4][4]; /* add `struct" to typedef */

void tt_drawFigure(tt_figure figure);

tt_figure tt_T = { {{0,0}, {0,1}, {0,2}, {1,1}},
{{0,1}, {1,0}, {1,1},{2,1}},
{{0,1}, {1,0}, {1,1},{1,2}},
{{0,0}, {1,0},{1,1}, {2,0}}
};

int main(void)
{
tt_drawFigure(tt_T);
return 0;
}

void tt_drawFigure(tt_figure figure)
{
int i, j;

for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++)
printf("%d, %d --- ", figure[i][j].x, figure[i][j].y);
printf("n");
}
/* Do your drawing */
}

Tambien puedes hacer

typedef struct tt_point
{
int x;
int y;
} tt_figure[4][4];

para que no te pierdas accidentalmente struct - como te perdiste cuando hiciste un typedef por separado.


2 para la respuesta № 2

Esto no es sintácticamente correcto:

typedef tt_point tt_figure[4][4];

A menos que haya creado previamente un typedef para tt_point, esto no podrá compilar. Lo que quieres es esto:

typedef struct tt_point tt_figure[4][4];

los struct la palabra clave es necesaria en cualquier lugar donde se use el tipo de estructura. Si haces esto, el código de ejemplo compila limpiamente.