3
I have two methods:
// Apenas define um vetor de 4 posições e retorna ele
int *verticesFromFace(int v1, int v2, int v3, int v4) {
int vertices[4] = {v1, v2, v3, v4};
return &(vertices);
}
// pega um cubo e uma face dele, retorna um vetor de vertices daquela face
Vertice *getPointsFromFace(Cube c, Face f) {
Vertice points[4];
int i;
for (i = 0; i < FACE_VERTICES; i++)
points[i] = c.vertices[f.vertices[i] - 1];
return &(points);
}
Example of use:
// pega uma face do cubo, armazena nela o vetor de vértices (usando aquele método) e também as cores daquela face (3 últimos parâmetros)
createFace(c.faces[0], verticesFromFace(1, 2, 3, 4), 1, 0, 0);
// Um vetor de vertices, quando chamo o metodo com o cubo e uma face do cubo
Vertice *points = getPointsFromFace(c, c.faces[i]);
GCC returns the error:
Warning: Function Returns address of local variable [-Wreturn-local-addr] Return &(points[0]);
Although it is a Warning, the code does not work because the local variable is not returned. How can I fix this?
P.S. &()
in return was an attempt I made when I saw a post on Stack Overflow English
I got it here, thanks, I just had to switch vector from 4 to pointer there on
malloc
. In case the release will be made in the variable that receives the return? Example :Vertice *points = getPointsFromFace(c, c.faces[i])
....free(points)
– Leonardo
That’s right. I didn’t understand what had to change.
– Maniero
Of
int vertices[4] = malloc(4 * sizeof(int));
forint *vertices = malloc(4 * sizeof(int));
– Leonardo
Ah, that’s right, I ate ball right there.
– Maniero