将结构数组作为指针传递给函数(C)

时间:2014-09-05 13:05:41

标签: c arrays pointers struct

我正在尝试编写一个函数来初始化数组中N个结构的所有值。我选择使用void函数并使用结构指针。我没有使用单结构指针的问题,但我无法弄清楚如何将指针地址传递给我的函数的结构数组。

以下代码会产生一个错误。

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

有没有人就如何修复我的语法提出建议,或者更好的方法来解决这个问题?

3 个答案:

答案 0 :(得分: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 

答案 1 :(得分: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;
    }
}

来自main的电话将变为:

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

答案 2 :(得分:1)

这一行:

election electionCandidates[candidates]; 

election结构类型的数组。对于数组,您不需要像以前一样通过引用显式传递:

Initialize(fp, candidates, &electionCandidates);

这样做:

Initialize(fp, candidates, electionCandidates);

在C数组中,通过引用自动传递。