2013-11-21 56 views
0

我有一個結構數組,結構中的一個元素是一個字符串,我需要將這些字符串與12個字符串數組中的其他字符串進行比較。 strcmp似乎不適合我。我知道我需要使用單獨的函數來比較字符串並將值作爲布爾值返回,但無法弄清楚如何使比較函數起作用。比較結構中的字符串和數組中的字符串

的結構

typedef struct{ 
    char *hometeam[Max_number_of_chars], *awayteam[Max_number_of_chars]; 
    int playround, date_day, date_month, date_year, 
      time_hour, time_minute, home_score, away_score, crowd_thousand, 
      crowd_hundred; 
    } match; 

char *teams[Number_of_teams] = {"AGF","AAB","SDR","RFC", 
            "EFB","BIF","SIF","OB", 
            "FCK","FCM", "ACH","FCN"}; 

,我需要的compare_function

if(compare_names(all_games[i].hometeam, teams[j])==0) {//crazy stuff} 

編輯該行的數組:我需要什麼正在功能比較幫助來自* teams [j]的字符串值與來自all_games [i] .hometeam的字符串值。但我不知道如何將struct all_games [i] .hometeam的特定部分傳遞給compare_function,我希望它是char字符串。

+0

請在發佈之前對代碼進行格式化。 – erbdex

回答

0
// Assuming char *teams[Number_of_teams] is globally defined. 
int find_match(struct match) 
{ 
    for(i=0; i < Number_of_teams; i++){ 
     if(strcmpi(match.hometeam, teams[i]) == 0){ 
      return i; 
     } 
    } 
    return -1; 
} 

你想要做什麼的邏輯流程並不清楚,但你可以嘗試像上面這樣的東西。

+0

好吧,也許我的邏輯不夠清楚,但現在問題歸結爲我不知道如何將'all_teams [i] .hometeam'作爲char *類型傳遞給我的函數,以便比較字符串在all_teams和來自團隊的字符串[j] – user3016762

+0

你想要函數只接受字符串而不是結構體? Change-find_match(char * team)。比較現在變成 - strcmpi(團​​隊,團隊[i]) – erbdex

+0

是的,我只想接收結構的字符串部分,但我也希望團隊[i]的特定字符串作爲輸入以及比較函數,因爲我已經有一個循環遍歷了另一個函數中的i和j,它們應該調用比較函數。在輸入之前,我一直在編譯器期望指定符的地方出現錯誤 – user3016762