C 将结构数组返回到main()中的变量
我有以下代码从输入文件中读取定义为结构的矩形数组。现在我想调用read_shapes_rpt函数,并将返回的值保存到main函数中的struct数组中 我是C语言的新手,刚刚开始理解指针之类的东西,我在尝试完成这项工作时遇到了很多错误。任何帮助都将不胜感激C 将结构数组返回到main()中的变量,c,arrays,struct,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
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
谢谢大家! struct Rectangle read\u shapes\u rpt-此函数返回指定给结构矩形数组指针的结构矩形类型的实例。struct Rectangle read\u shapes\u rpt-此函数返回指定给结构矩形数组指针的结构矩形类型的实例。读取_shapes_rpt返回单个结构矩形,但尝试将其分配给指向结构矩形的指针数组。你不能做那样的作业
您可能想做的是将rect_数组和数组的大小一起传递给读取_shapes_rpt并处理它,然后去掉shape_列表
在本例中,返回指向局部变量的指针return*shape\u list从来都不是一件好事,因为该结构的内存位于堆栈上,并且一旦函数返回就不可用。您定义了read\u shapes\u rpt以返回单个结构矩形,但尝试将其分配给指向结构矩形的指针数组。你不能做那样的作业
您可能想做的是将rect_数组和数组的大小一起传递给读取_shapes_rpt并处理它,然后去掉shape_列表
在本例中,返回指向局部变量的指针return*shape_list从来都不是一件好事,因为该结构的内存位于堆栈上,一旦函数返回,就无法使用。您将函数声明为返回单个结构;不能将其分配给指向结构类型的整个指针数组。您需要仔细研究内存管理。不能从函数返回数组。您可能应该将结构数组而不是指向结构的指针传递到函数中,函数的返回值应该是数组中有效条目的数目。您还应该将数组的大小传递给函数,这样它就不会写越界,从而导致堆栈溢出
#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;
}
请注意,将文件名传递给函数既可以泛化函数,同时也可以方便地有意义地报告错误,而无需重复您自己或文件名。如图所示传递数组大小使用C99功能。如果您一直使用C89/90编译器,您可以将函数参数更改为struct Rectangle*shape\u list,如果您在Windows上工作,则可能是这样。您将函数声明为返回单个结构;不能将其分配给指向结构类型的整个指针数组。您需要仔细研究内存管理。不能从函数返回数组。您可能应该将结构数组而不是指向结构的指针传递到函数中,函数的返回值应该是数组中有效条目的数目。您还应该将数组的大小传递给函数,这样它就不会写越界,从而导致堆栈溢出
#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;
}
请注意,将文件名传递给函数既可以泛化函数,同时也可以方便地有意义地报告错误,而无需重复您自己或文件名。如图所示传递数组大小使用C99功能。如果您一直使用C89/90编译器,您可以将函数参数更改为struct Rectangle*shape\u list,如果您在Windows上工作,可能就是这样。您声明函数以返回单个结构;不能将其分配给指向结构类型的整个指针数组。您需要仔细研究内存管理。不能从函数返回数组。您可能应该将结构数组而不是指向结构的指针传递到函数中,函数的返回值应该是数组中有效条目的数目。您还应该将数组的大小传递给函数,使其不会写越界,从而导致堆栈溢出。将如此大的数组放在堆栈上有点风险,无论是read_shapes_rpt中的结构数组还是main中的结构指针数组。最好在main中使用malloc分配内存,并将其传递给read_shapes_rpt;不能将其分配给指向结构类型的整个指针数组。您需要仔细研究内存管理。不能从函数返回数组。您可能应该将结构数组而不是指向结构的指针传递到函数中,函数的返回值应该是数组中有效条目的数目
. 您还应该将数组的大小传递给函数,使其不会写越界,从而导致堆栈溢出。将如此大的数组放在堆栈上有点风险,无论是read_shapes_rpt中的结构数组还是main中的结构指针数组。最好在main中使用malloc分配内存,并将其传递给read_shapes_rpt。返回指向自动变量的指针总是一件非常糟糕的事情!。它是使用它的代码和重用堆栈的代码的炸弹同时尝试在堆栈上存储如此多的数据是非常糟糕的,如果你修复指针错误,那么你会得到一个很好的堆栈溢出异常,这次不是这个站点的名称。返回指向自动变量的指针总是一件非常糟糕的事!。它是使用它的代码和重用堆栈的代码的炸弹并且尝试在堆栈上存储这么多数据是非常糟糕的,如果你修复了指针错误,那么你会得到一个很好的堆栈溢出异常,这次不是这个站点的名称。