gpt4 book ai didi

c - 如何将从文本文件读取的整数传递到结构函数中?

转载 作者:行者123 更新时间:2023-11-30 16:05:54 25 4
gpt4 key购买 nike

我想将整数的 txt 文件传递​​到smallest_xcoord 结构中。我正在尝试获得凸包。我的问题是,我在尝试时不断收到错误。如何将从文本文件读取的整数传递到我的结构函数中?我没有包含我的 struct Point 但它存储了一个 int x;和 int y; (对于凸包点)。

    ```
//GLOBAL Variable
static int integers[100000];

int load_integers(const char* filename)
{
//int integers[100000];

FILE *fp = NULL;
int value;
int i = -1;

if ((fp= fopen (filename, "r"))==NULL)
{
return -1;
}

while (fscanf (fp, "%u", &value)&& !feof(fp) && ++i <100000)
{
integers[i] = value;
}
fclose(fp);

return i+1;
}

struct Point smallest_xCoord (struct Point points, int numPoints)
{
struct Point points[];

struct Point minX_point = points[0];
int i;

for(i=1;i<numPoints;i++)
{
if ((points[i].x) < (minX_point.x))
{
minX_point = points[i];
}
else if (points[i].x == minX_point.x)
{
if (points[i].y < minX_point.y)
{
minX_point=points[i];
}
}
}
return minX_point;
}

int main(int argc, char *argv[])
{

int num_ints=0;

if (argc <2)
{
fprintf(stderr, "%s <file>\n",argv[0]);
return -1;
}

if ((num_ints = load_integers(argv[1])) <=0)
{
fprintf(stderr, "Error reading from file: %s\n",argv[1]);
return -1;
}

smallest_xCoord(integers, num_ints);

return 0;
}
```

最佳答案

smallest_xCoord(整数, num_ints);

首先,您不能将整数数据类型传递给“Point”数据类型参数。其次,您不能将数组整数数据类型传递给单个 Point 参数。

struct Pointsmallest_xCoord(structPointpoints,intnumPoints) { struct Point点[];

第三,您无法在 x_Coord 内部创建与其参数名称相似的新变量,因为您要重新声明它。

如果你想传递一个数组作为参数,你必须在它的名字之前使用一个指针(*)。为了使smallest_xCoord函数起作用,您需要指定要比较或分配给Point结构的哪个成员。我给您的建议是,您可能需要将该全局 int 数组变量更改为全局 Point 数组变量,并使用文件中的值加载它,我假设该文件应该包含 x 和 y 坐标的值,以便您可以比较加载的全局值将数组指向您提供的数组。然后,将smallest_xCoord 的参数更改为“struct Point *points”或“struct Pointpoints[]”,因为您要传递要比较的数组。

关于c - 如何将从文本文件读取的整数传递到结构函数中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60132194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com