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

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

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

분류에서Dev

Cuda passing an array of structs

분류에서Dev

bsearch with array of pointer of structs

분류에서Dev

Passing a pointer to a function

분류에서Dev

Passing a pointer to one C function as a parameter to another function

분류에서Dev

ANSI C - passing data from function to a CHAR pointer

분류에서Dev

C++, Passing non-static function pointer in a class constructor

분류에서Dev

C++ passing array of pointers to function

분류에서Dev

Passing an array to a function

분류에서Dev

Pointer to array of struct in function

분류에서Dev

Passing an array and a variable to a function in Perl

분류에서Dev

Error in passing and modifying a array to a function

분류에서Dev

How to correctly allocate memory for an array of structs in C?

분류에서Dev

C *char pointer to char array

분류에서Dev

Wrap c++ function that needs function pointer

분류에서Dev

Accessing a triple pointer inside a function with array notation

분류에서Dev

Create Dynamically Allocated Array with Pointers to Structs C++

분류에서Dev

Function pointer declaration works in C but not in C++

분류에서Dev

Swapping 2d array (pointer to pointer) in C

분류에서Dev

Array of structs not adding

분류에서Dev

C++: Pointer value changed exiting a function

분류에서Dev

Allocating memory for a pointer inside a function call in C

분류에서Dev

c++ return a pointer to a struct from a function

분류에서Dev

Pointer Array Sorting Algorithm in C++

분류에서Dev

Why the size of pointer to array is always 8 in C?

분류에서Dev

C++ copying array into pointer argument

분류에서Dev

Character array in c with function

분류에서Dev

Call function for all array elements without passing item as parameter

분류에서Dev

Syntax error passing SQL result to PostgreSQL function accepting array

Related 관련 기사

  1. 1

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

  2. 2

    Cuda passing an array of structs

  3. 3

    bsearch with array of pointer of structs

  4. 4

    Passing a pointer to a function

  5. 5

    Passing a pointer to one C function as a parameter to another function

  6. 6

    ANSI C - passing data from function to a CHAR pointer

  7. 7

    C++, Passing non-static function pointer in a class constructor

  8. 8

    C++ passing array of pointers to function

  9. 9

    Passing an array to a function

  10. 10

    Pointer to array of struct in function

  11. 11

    Passing an array and a variable to a function in Perl

  12. 12

    Error in passing and modifying a array to a function

  13. 13

    How to correctly allocate memory for an array of structs in C?

  14. 14

    C *char pointer to char array

  15. 15

    Wrap c++ function that needs function pointer

  16. 16

    Accessing a triple pointer inside a function with array notation

  17. 17

    Create Dynamically Allocated Array with Pointers to Structs C++

  18. 18

    Function pointer declaration works in C but not in C++

  19. 19

    Swapping 2d array (pointer to pointer) in C

  20. 20

    Array of structs not adding

  21. 21

    C++: Pointer value changed exiting a function

  22. 22

    Allocating memory for a pointer inside a function call in C

  23. 23

    c++ return a pointer to a struct from a function

  24. 24

    Pointer Array Sorting Algorithm in C++

  25. 25

    Why the size of pointer to array is always 8 in C?

  26. 26

    C++ copying array into pointer argument

  27. 27

    Character array in c with function

  28. 28

    Call function for all array elements without passing item as parameter

  29. 29

    Syntax error passing SQL result to PostgreSQL function accepting array

뜨겁다태그

보관