c - 传递给另一个方法的结构体数组

标签 c arrays structure

我在一个函数中创建了结构数组,并且想在另一个文件中使用该结构数组。我的结构是这样的:

struct competitors{
int competitorNumber;
char registeredCourse;
char name[50];
};

编辑:抱歉我复制了错误的结构!!!

这就是我填充结构的方式:

lines = lineCount(fileName);
struct checkPoints checkPoint[lines];
sizeOfCheckPoints = lines;
chPo = fopen(fileName, mode);
if (chPo == NULL) {
    printf("Can't find the files.");
    exit(1);
} else {
    for (i = 0; i < lines; i++) {
        fscanf(chPo, "%c %d %d %d:%d\n", &checkPoint[i].dropOut, &checkPoint[i].currentPoint, &checkPoint[i].competitor, &checkPoint[i].hour, &checkPoint[i].minute);
    }
}

它的结构填充得很好,但我不知道应该如何在另一个文件中使用它。这就是我尝试使用它的方法,但似乎不起作用:

for(i = 0; i<sizeOfCompetitors; i++){
    if (name == competitor[i].name){
        printf("Here is comp details: %d\t%c\t%s", competitor[i].competitorNumber, competitor[i].registeredCourse, competitor[i].name);
    }else{
        printf("%s was not found", name);
    }
}

有人可以帮我吗?

最佳答案

您希望在 .h 文件中定义结构体,以便两个 .c 文件都知道它。

然后你就可以使用它了!最好的方法是将其从第一个文件传递到第二个文件。您可能也想在 .h 文件中声明您的方法 例如 /* 检查点.h / / 你的结构 def / / 也许是它的 typedef / / 方法声明 */ void doSomething(struct checkPoints *points, int numPounts);

/* second dot C */
#include "checkpoint.h"
void doSomething(struct checkPoints *points, int numPounts)
{
    int i;
    for(i = 0; i < numPoints; i++)
    {
        int currentPoint = points[i].currentpoint;
        ...

关于c - 传递给另一个方法的结构体数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13852170/

相关文章:

c++ - 如何在c/c++中实现挂载功能

c - 使用 gcc 时 undefined reference

c - 请在这里解释 typedef 的行为

c++ - 如何在 VC 6.0 中对结构数组进行排序

c++ - 如何在 C/C++ 中获取多维数组的列?

c - 段错误 - 无效的自由未解决 - C

sql - Postgres 合并为空的 JSONB 数组

javascript - 将键值对追加到数组的特定索引处

php foreach循环不遍历整个数组

C中发送大量值的代码结构