user3349184
user3349184

Reputation: 143

Struct C++ array in function parameters not working at all

hello i have to do a program using an array of structures.. and i have to initialize it in a function. below i am trying, but my prototype keeps getting error "Expected primary expression".. i have followed tutorials but cant figure out what im doing wrong please help. i cant use pointers or vectors.. just basic stuff thank you for your time

struct gameCases{

bool flag = false;
int casenum;
double value;
};
 int initialize(gameCases cases);  //prototype

--- main()

gameCases cases[26];
initialize(cases);    //call

int initialize(gameCases cases)  //definition
{
double values[26] = {.01, 1, 5, 10, 25, 50,
                75, 100, 200, 300, 400, 500, 750, 1000,
                5000, 10000 , 25000, 50000, 75000, 100000,
                200000 , 300000, 400000, 500000,
                1000000, 2000000};

for (int i = 0; i < 26; i++)
{
    array[i].value = values[i];
}
}

Upvotes: 0

Views: 143

Answers (2)

coyotte508
coyotte508

Reputation: 9695

int initialize(gameCases cases[26]);  //prototype

int initialize(gameCases cases[26])  //definition
{
    double values[26] = {.01, 1, 5, 10, 25, 50,
            75, 100, 200, 300, 400, 500, 750, 1000,
            5000, 10000 , 25000, 50000, 75000, 100000,
            200000 , 300000, 400000, 500000,
            1000000, 2000000};

    for (int i = 0; i < 26; i++)
    {
        cases[i].value = values[i];
    }
}

and to call:

initialize(cases); 

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

Declare the function like

int initialize( gameCases *array, size_t n );  

and call it like

initialize( cases, 26 );    

Or you could pass the array by reference. For example

int initialize( gameCases ( &cases )[26] );

Take into account that the function is declared as having return type int but it acrually returns nothing.

Upvotes: 1

Related Questions