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

River

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?

Klas Lindbäck

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing an array of structs to a function?

From Dev

Passing an array of structs to a function?

From Dev

Passing a pointer to a char array as an argument to a function - C

From Dev

Passing a pointer to a char array as an argument to a function - C

From Dev

Passing in an element in an array as a pointer in a function in C

From Dev

Error when passing pointer to array of structs

From Dev

Passing a pointer to an array to a function

From Dev

C - Pointer to dynamic array of structs

From Dev

C - Passing pointer to function

From Dev

Call function by function pointer from array of structs

From Dev

Passing Array Into Function - Pointer vs Reference (C++ vs C)

From Dev

Pointer to array of structs element field in a function

From Dev

Passing a char pointer array to a function

From Dev

Passing pointer of char array to function

From Dev

Create 2D array by passing pointer to function in c

From Dev

Passing a multidimensional array as a pointer to a function in C gives an unexpected result

From Dev

C: How to add structs to pointer array

From Dev

C++ passing function pointer

From Dev

Passing Function Pointer in C#

From Dev

passing a pointer through a function c

From Dev

Passing a pointer (string) to a C function

From Dev

Passing an array/pointer into a stuct in c

From Dev

passing array (not pointer) to a struct in c

From Dev

Passing String Pointer Array in C

From Dev

passing a pointer to a pointer in c function by value

From Dev

Passing single pointer and double pointer to a function in c

From Dev

passing function pointer in c with a pointer argument

From Dev

Function pointer array, passing values defined in array

From Dev

passing a vector of structs into a function

Related Related

  1. 1

    Passing an array of structs to a function?

  2. 2

    Passing an array of structs to a function?

  3. 3

    Passing a pointer to a char array as an argument to a function - C

  4. 4

    Passing a pointer to a char array as an argument to a function - C

  5. 5

    Passing in an element in an array as a pointer in a function in C

  6. 6

    Error when passing pointer to array of structs

  7. 7

    Passing a pointer to an array to a function

  8. 8

    C - Pointer to dynamic array of structs

  9. 9

    C - Passing pointer to function

  10. 10

    Call function by function pointer from array of structs

  11. 11

    Passing Array Into Function - Pointer vs Reference (C++ vs C)

  12. 12

    Pointer to array of structs element field in a function

  13. 13

    Passing a char pointer array to a function

  14. 14

    Passing pointer of char array to function

  15. 15

    Create 2D array by passing pointer to function in c

  16. 16

    Passing a multidimensional array as a pointer to a function in C gives an unexpected result

  17. 17

    C: How to add structs to pointer array

  18. 18

    C++ passing function pointer

  19. 19

    Passing Function Pointer in C#

  20. 20

    passing a pointer through a function c

  21. 21

    Passing a pointer (string) to a C function

  22. 22

    Passing an array/pointer into a stuct in c

  23. 23

    passing array (not pointer) to a struct in c

  24. 24

    Passing String Pointer Array in C

  25. 25

    passing a pointer to a pointer in c function by value

  26. 26

    Passing single pointer and double pointer to a function in c

  27. 27

    passing function pointer in c with a pointer argument

  28. 28

    Function pointer array, passing values defined in array

  29. 29

    passing a vector of structs into a function

HotTag

Archive