将struct数组返回到main()中的变量

时间:2015-07-31 19:00:47

标签: c arrays struct

我有以下代码来读取矩形数组(从输入文件定义为结构)。我现在希望调用read_shapes_rpt()函数并将返回的值保存到main()函数中的struct数组中。

我是C的新手,刚开始理解指针之类的东西,我在尝试完成这项工作时遇到了一些错误。任何帮助将不胜感激!

typedef struct Rectangle
    {
    float ll_x;
    float ll_y;
    float ur_x;
    float ur_y; 
    } Rectangle;

struct Rectangle read_shapes_rpt()
{
    FILE *f = fopen("check_pg_stapler_shapes.rpt", "r") ;
    struct Rectangle shape_list[100000];

    float temp_ll_x, temp_ll_y, temp_ur_x, temp_ur_y;
    int i = 0;

    while (fscanf(f, "%f,%f,%f,%f", &temp_ll_x, &temp_ll_y, &temp_ur_x, &temp_ur_y) != EOF) {
        shape_list[i].ll_x = temp_ll_x;
        shape_list[i].ll_y = temp_ll_y;
        shape_list[i].ur_x = temp_ur_x;
        shape_list[i].ur_y = temp_ur_y;
        printf("%f,%f,%f,%f\n", temp_ll_x, temp_ll_y, temp_ur_x, temp_ur_y);
        i++;
    }

    fclose(f);
    return *shape_list;
}

int main()
{   
    struct Rectangle *rect_array[100000];
    rect_array = read_shapes_rpt();
    return 0;
}

错误消息如下:

optimize_via_pgStapler.c: In function 'main':
optimize_via_pgStapler.c:38: error: incompatible types in assignment

谢谢!

3 个答案:

答案 0 :(得分:2)

您定义了read_shapes_rpt以返回单个struct Rectangle,但您正在尝试将其分配给指向struct Rectangle的指针数组。你不能做那样的任务。

您可能想要做的是将rect_array(以及数组的大小)传递给read_shapes_rpt并继续处理,然后摆脱shape_list

返回指向局部变量的指针(在本例中为return *shape_list)从来都不是一件好事,因为该结构的内存存在于堆栈中,一旦函数返回就无法使用。

答案 1 :(得分:1)

struct Rectangle read_shapes_rpt() - 此函数返回类型struct Rectangle的实例,您将其分配给指向struct Rectangle数组的指针。

答案 2 :(得分:1)

You declare the function to return a single structure; you can't assign that to a whole array of pointers to the structure type. You need to look at the memory management hard. You can't return arrays from functions. You should probably pass an array of the structure (not of pointers to the structure) into the function, and the return value from the function should be the number of valid entries in the array. You should also pass the size of the array to the function so it does not write out of bounds, causing a stack overflow.

#include <stdio.h>

typedef struct Rectangle
{
    float ll_x;
    float ll_y;
    float ur_x;
    float ur_y; 
} Rectangle;

int read_shapes_rpt(const char *filename, int n, struct Rectangle shape_list[n])
{
    FILE *f = fopen(filename, "r") ;
    if (f == 0)
    {
        fprintf(stderr, "Failed to open file %s for reading\n", filename);
        return -1;
    }

    float temp_ll_x, temp_ll_y, temp_ur_x, temp_ur_y;
    int i = 0;

    while (i < n && fscanf(f, "%f,%f,%f,%f", &temp_ll_x, &temp_ll_y, &temp_ur_x, &temp_ur_y) == 4) {
        shape_list[i].ll_x = temp_ll_x;
        shape_list[i].ll_y = temp_ll_y;
        shape_list[i].ur_x = temp_ur_x;
        shape_list[i].ur_y = temp_ur_y;
        printf("%f,%f,%f,%f\n", temp_ll_x, temp_ll_y, temp_ur_x, temp_ur_y);
        i++;
    }

    fclose(f);
    return i;
}

int main(void)
{   
    enum { MAX_SHAPES = 100000 };
    struct Rectangle rect_array[MAX_SHAPES];
    int n_shapes = read_shapes_rpt("check_pg_stapler_shapes.rpt", MAX_SHAPES, rect_array);

    if (n_shapes > 0)
    {
        /* Use the shapes that were read */
    }

    return 0;
}

Note that passing the file name to the function both generalizes the function and also (coincidentally) makes it easier to report errors meaningfully without repeating yourself (or the name of the file). Passing the array size as shown uses a C99 feature. You can change the function parameter to struct Rectangle *shape_list if you are stuck with a C89/90 compiler — which you might be if you work on Windows.