2012-12-13 33 views
0

我在其中一個函數中創建了一個結構數組,我想在另一個文件中使用該數組結構。我的結構是這樣的:傳遞給另一個方法的結構數組

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); 
    } 
} 

誰能幫我嗎?

+0

'.competitorNumber'在哪裏?或競爭對手陣列?或'.name'? – perreal

+0

結構本身就是一個數組,因此'competitor [i]'(因爲它顯示了我的代碼的第二位,'competitorNumber'和'name'保存在'struct competitor'(代碼的第一位)。基本上我的問題是:如何訪問不同文件中的結構數組 – Shepard

+0

什麼是競爭者和名稱? –

回答

1

你希望你的結構定義在一個.h文件中,這樣兩個.c文件都知道它。

然後你就可以使用它!最好的辦法是將它從第一個文件傳遞到第二個文件。您可能想要在.h文件中聲明您的方法,例如 /* checkpoint.h / /你的結構DEF / /也許它/ /的方法*/ 無效DoSomething的(結構關卡*點,詮釋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; 
     ... 
相關問題