0

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

0

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you but the post says no pointers. can you devise a way to do this without pointers, Vlad?
@user3349184 You marked the post as best where the function parameter is a pointer though you said that a pointer shall not be used.:) This declaration gameCases cases[26] is equivalent to gameCases *cases The only correct solution is to use reference as I showed.
0
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); 

3 Comments

is call ... initialize(cases[]) ??
updated my post. But really, the first solution given by Vlad from Moscow is the better one.
in your case with your restrictions yes, in real life no

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.