In C++ using gc++ compiler (netbeans, ubuntu), I am trying to pass a struct into a function (working fine), operate on an array in the struct (fine). But when I leave the function (which is void return value), the struct values snap back to their initialized values.

the struct in a header file:

struct user {
    char name[256];
    double cash;
    unsigned int handindex[12];
};

initialized values (just to help me visually):

int main{
     user player, dealer;
         for(int i = 0; i < 13; i++){
            player.handindex[i] = 99;
            dealer.handindex[i] = 99;
         }
...

prototype:

void deal       (user, user, card[]);

pass the array into a function:

...
deal(player, dealer, deck);
...

do a modification:

void deal(user player, user dealer, card deck[]){
        player.handindex[0] = 3;
}

The above is a mock modification. I've debugged up to the last line of the function that is modifying the handindex array, and its fine. For instance, this one would show:

DEBUGGER----------------------
player.handindex = {3,99,99,99,99,99,99,99,99,99,99,99}

but then i step into the next line (which returns to main to continue execution), and the player.handindex returns to all 99s.

Am I not passing the struct correctly? I really dont know whats going on...

Edited 6 Years Ago by samurai1200: n/a

The prototype needs to be void deal (user&, user&, card[]); if you want to modify the details of the user and dealer.

Halelujah, it worked!

To anyone else reading this, don't forget that the declaration also needs to change:

void deal(user& player, user& dealer, card deck[]){
...
}
This question has already been answered. Start a new discussion instead.