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;


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:

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...

Recommended Answers

All 3 Replies

You are passing by value. Try passing by reference.

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[]){
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.