2014-09-05 131 views
-1

我試圖寫將初始化在數組n結構的所有值的函數。我選擇使用void函數並使用結構指針。我有使用單一結構的指針沒有問題,但我無法弄清楚如何將指針地址傳遞給結構的陣列,以我的功能。傳遞結構的數組的函數作爲指針(C)

下面的代碼產生單一錯誤。

typedef struct candidate { 
    char name[20]; //name of the election candidate 
    int votes; //amount of votes the candidate has 
} election; 

void Initialize(FILE *fp, int candidates, election *electionCandidates[]); 

int main(void) 
{ 
    const int candidates = 7; //this will be the amount of structs initialized 
    const int voters = 365; //this will be the N iterations of a for loop for the voting process 

    FILE *fp = fopen ("elections.txt", "R"); //save file pointer for use when taking formatted input 

    election electionCandidates[candidates]; //declare 'candidates' structs, one for each candidate in the election 

    Initialize(fp, candidates, &electionCandidates); //save candidate names and set votes = to 0 

    fclose(fp); 
    return 0; 
} 

void Initialize(FILE *fp, int candidates, election *electionCandidates[]) //init values of the candidate struct array by passing pointer to void function 
{ 
    int eN = 0, N = candidates; //eN = executed number of for loop iterations, N = total number of iterations to be completed 
    for (eN = 0; eN < N; eN ++) 
    { 
     char name[20] = ""; 
     fscanf (fp, "%s", &name); 
     strcpy(electionCandidates[eN]->name, name); 
     electionCandidates[eN]->votes = 0; 
    } 
} 

我有指向該行的錯誤:

Initialize(fp, candidates, &electionCandidates); //save candidate names and set votes = to 0 

有沒有人對如何解決我的語法,或者更好的辦法去了解這個建議嗎?

回答

1

因爲你有一個指向數組的指針你得到一個錯誤,你把它當作內部Initialize指針數組。

在你的情況,你可以簡單地傳遞一個簡單的指針:

void Initialize(FILE *fp, int candidates, election *electionCandidates) //init values of the candidate struct array by passing pointer to void function 
{ 
    int eN = 0, N = candidates; //eN = executed number of for loop iterations, N = total number of iterations to be completed 
    for (eN = 0; eN < N; eN ++) 
    { 
     char name[20] = ""; 
     fscanf (fp, "%s", &name); 
     strcpy(electionCandidates[eN].name, name); 
     electionCandidates[eN].votes = 0; 
    } 
} 

從主的通話將變爲:

Initialize(fp, candidates, electionCandidates); //save candidate names and set votes = to 0 
+0

將'char name [20] =「」;'改爲'char * name = malloc(20)'和'fscanf(fp,「%s」,&name);'''fscanf(fp,「%s」, &name)'也是一個好主意,請把它包含在你的答案中 – Igor 2014-09-05 13:36:37

+0

謝謝!這個編譯和應該工作,還沒有用一個輸入文件測試它看起來像我複雜它拋出所有的指針語法我記得不知道什麼時候可以使用它,需要刷一下:B – River 2014-09-05 13:41:03

2

傳遞陣列使用指針來第一個項目在裏面,這是數組名已經存在。 首先改變你的函數聲明來自:

void Initialize(FILE *fp, int candidates, election *electionCandidates[]); 

void Initialize(FILE *fp, int candidates, election *electionCandidates); 

,並調用它像:

Initialize(fp, candidates, electionCandidates); 

另一件事是,你訪問結構項目的成員在一個數組它是指針數組。改爲使用.運算符。

這就是你應該做的,使其工作。現在我要告訴你,你搞什麼名堂來完成:

  • 在函數聲明election *electionCandidates[]是一個指針類型的指針election
  • 在主功能&electionCandidateselection
  • 類型的指針地址

而在你的函數體electionCandidates是一個指向數組的指針不是一個數組,這就是爲什麼如果你要訪問數組的元素,你應該調用是這樣的:

(*electionCandidates)[eN].name 
+0

感謝您的深入解釋!我確實想知道我錯在哪裏,我認爲當我設置一個指針作爲參數時,我會有給它一個指針地址,當我調用函數時,而我真正需要做的是給它指向的東西。 絕對感覺就像我現在得到它! – River 2014-09-05 13:52:12

1

這條線:

election electionCandidates[candidates]; 

election結構類型的陣列。對於數組,你不需要明確地按引用傳遞就像你正在做的:

Initialize(fp, candidates, &electionCandidates); 

只是這樣做:

Initialize(fp, candidates, electionCandidates); 

在C數組通過引用自動傳遞。