简体   繁体   中英

Why does the value of a pointer change after passing it to a function that does not modify the pointer?

The pointer in main() , ptrTop , is initialized to point to int topDeck = 1 . Every time I run the program, the value of the (I think) dereferenced pointer changes to a different number. I believe that the problem lies in the deal() function. If I comment out the call to deal() in main() , the value of my pointer is not modified. I did not write any statements that change the value of the pointer. I wrote comments around the parts of the code that might be relevant to the question.

/* 
 * This is a card dealing program. 
 * the fucntion deal() keeps changing the value of 
 * the int pointer ptrTop declared in main(). sometimes, 
 * the value is what it is sopposed to be (1), but when i run 
 * the program again, it is a different value. I used 
 * gcc (GCC) 4.9.2 20150212 (Red Hat 4.9.2-6)
 */

int main(void) {

    int topDeck = 1;
    int *ptrTop = &topDeck;

    //this ptrTop is sopposed to keep track of the last
    //card dealt, but it randomly changes every time I run 

    unsigned int handSuit[5] = {0};
    unsigned int handFace[5] = {0};



    unsigned int deck[SUITS][FACES] = {0};


    deal(deck, face, suit, ptrTop, handSuit, handFace);

    printf("%i\n", *ptrTop);
    // If print the value while I comment out the deal() in line 55, 
    // the value of * ptrTop does not change.
    // this gives me reason to believe that deal() is causing the trouble.

}


void deal(unsigned int wDeck[][FACES], const char *wFace[], const char *wSuit[], 
    int *ptrTop, unsigned int handSuit[], unsigned int handFace[]) 
{
    size_t card;
    size_t row;
    size_t column;
    int top = *ptrTop;
    // i have to use top because if i don't the dea() function will print
    // more than 5 cards (it is sopposed to print a 5 card hand.

    // these for loops loop through the double scripted array wDeck
    for (card = top; card <= top + 4; ++card) {

        for (row = 0; row < SUITS; ++row) {

            for(column = 0; column < FACES; ++column) {

                if(wDeck[row][column] == card) {
                    printf( "%s of %s \n", wFace[ column ], wSuit[ row ]);
                    handFace[card] = column;
                    handSuit[card] = row;
                }
            }
        }
    }
    // *ptrTop = card;
    // the value of *ptrTop consistently becomes six if line above is uncommented.
    // I would think that the value should still be 1 
    // when the program is run in this state.
}

This obscure loop is the cause:

 for (card = top; card <= top + 4; ++card)

Card gets an index from 1 to 5, while you have variables

unsigned int handSuit[5] = {0};
unsigned int handFace[5] = {0};

That only supports index 0 to 4. You access these arrays out-of-bounds, and as a side effect of that undefined behavior, you overwrite other variables.

I'm not really sure what other value (than six) of *ptrTop you expect; let's take a look at deal :

for (card = top; card <= top + 4; ++card)

and remember, this is the value of top , as initialized in main :

int topDeck = 1;
int *ptrTop = &topDeck;

So, in deal , you loop five times, incrementing card , then in the last iteration the for loop will increment card for the fifth time, see that it is 6 , and since 6 <= 5 is false, it will quit, then if you do *ptrTop = card; , *ptrTop is indeed consistently equal to six.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM