简体   繁体   中英

Passing Array of Structs to a Function as a Pointer (C)

I am attempting to write a function that will initialize all values of N structs in an array. I chose to use a void function and use a structure pointer. I have no issue with using single structure pointers, but I cannot figure out how to pass a pointer address to an array of structs to my function.

The following code produces a single error.

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

The error I have points to this line:

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

Does anyone have advice on how to fix my syntax, or a better way to go about this?

Passing arrays use a pointer to first item in it, which is the array name already. First change your function declaration from:

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

to

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

and call it like that:

Initialize(fp, candidates, electionCandidates);

Another thing is that you access members of struct items in an array as it is array of pointers. Use . operator instead.

That is what you should do to make it works. Now I'll tell you what've you done:

  • election *electionCandidates[] in a function declaration is a pointer to a pointer of type election
  • &electionCandidates in your main function is an address to a pointer of type election

And in your function body electionCandidates is a pointer to an array not an array, that's why if you want access an element of the array you should call something like:

(*electionCandidates)[eN].name 

You get an error because you have a pointer to an array and you treat it as an array of pointers inside Initialize .

In your case you can simply pass a simple pointer:

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

The call from main will become:

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

This line:

election electionCandidates[candidates]; 

is an array of election struct type. For array you do not need to explicitly pass by reference like you are doing:

Initialize(fp, candidates, &electionCandidates);

Just do this:

Initialize(fp, candidates, electionCandidates);

In C arrays are automatically passed by reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM