Question

Return an array of objects from a C function?

I'd like to know how to return an array of objects from a C function.

Here's an example:

SDL_Vertex initTriangle (struct Point2D point1, struct Point2D point2, struct Point2D point3) {
   SDL_Vertex triangleVertex[3]=
        {
            {
                { new_point1.x,new_point1.y },
                { 255, 255, 0, 0xFF },
                { 0.f, 0.f }
            },
            {
                { new_point2.x, new_point2.y },
                { 0,255,255, 0xFF },
                { 0.f, 0.f }
            },
            {
                { new_point3.x, new_point3.y },
                { 255,0,255, 0xFF },
                { 0.f, 0.f }
            }
        };
   return triangleVertex;
};

SDL_Vertex triangleVertex[3] = initTriangle(...);
SDL_RenderGeometry(renderer, NULL, triangleVertex, 3, NULL, 0);

I'd like to do something like that, but I need to make the function return an object as if I was making this :

SDL_Vertex triangleVertex[3]=
        {
            {
                { new_point1.x,new_point1.y },
                { 255, 255, 0, 0xFF },
                { 0.f, 0.f }
            },
            {
                { new_point2.x, new_point2.y },
                { 0,255,255, 0xFF },
                { 0.f, 0.f }
            },
            {
                { new_point3.x, new_point3.y },
                { 255,0,255, 0xFF },
                { 0.f, 0.f }
            }
        };
SDL_RenderGeometry(renderer, NULL, triangleVertex, 3, NULL, 0);
 2  82  2
1 Jan 1970

Solution

 5

It looks like you want initTriangle to return an array of 3 SDL_Vertexs.

You cannot return an array this way (by value) from a C function.

One solution would be to accept it as a parameter and fill it in the function:

void initTriangle (struct Point2D new_point1, 
                   struct Point2D new_point2, 
                   struct Point2D new_point3, 
                   SDL_Vertex triangleVertex[3])  // <- add this parameter
{
    // Fill the array passed in:
    triangleVertex[0] = 
            (SDL_Vertex)  // This cast might be needed
            {
                { new_point1.x,new_point1.y },
                { 255, 255, 0, 0xFF },
                { 0.f, 0.f }
            };
    triangleVertex[1] = 
            (SDL_Vertex)  // This cast might be needed
            {
                { new_point2.x, new_point2.y },
                { 0,255,255, 0xFF },
                { 0.f, 0.f }
            };
    triangleVertex[2] = 
            (SDL_Vertex)  // This cast might be needed
            {
                { new_point3.x, new_point3.y },
                { 255,0,255, 0xFF },
                { 0.f, 0.f }
            };
};

Example for usage (the array of the caller is passed as the last argument):

SDL_Vertex myTriangleVertex[3];
initTriangle(..., myTriangleVertex);  

Another alternative is to allocate the array inside the function on the heap, and return it. But this would require the caller to remember to deallocate the array to avoid a memory leak.

A third alternative is to embed the array in a struct and then you'll be able to return it from the function.

But in this case I would go with the first solution I suggested (at least by default), being the simplest one.

2024-07-24
wohlstad