Array/Pointer is causing a STATUS_ACCESS_VIOLATION is g++

Discussion in 'C++' started by SB, Oct 30, 2003.

  1. SB

    SB Guest

    Hello! I have two classes, Player and CardDeck. CardDeck contains an int
    array called drawCards(int drawCards[12];). Among other things, the Player
    class contains a static CardDeck object(static CardDeck c;). These are both
    in a header file. From my other .cpp file with main, I have an array of
    players (Player players[11];). My goal is to give access to a single deck of
    cards (drawCards) that was initialized in the CardDeck class to the Player
    class - obviously, it has to be static because all 11 players need to share
    the same deck of cards - not each have their own deck. I was able to
    accomplish this and have verified the values in the drawCard array in the
    Player class are matching the values of the drawCards deck in the CardDeck
    class.
    Now for the problem...everything worked fine until I'm assigning each player
    from the player array a card (an int value) out of the drawCards array.
    Everytime it will assign 9 players a card, then I get this message:
    (actually, I'll paste the output to show that the first 9 are getting a
    card)
    a draws a 5 of Spades
    b draws a Jack of Spades
    c draws a 7 of Spades
    d draws a 6 of Spades
    e draws a 2 of Spades
    f draws a Queen of Spades
    g draws a 8 of Spades
    h draws a King of Spades
    i draws a 9 of Spades
    [main] C:\Program Files\Microsoft Visual Studio\MyProjects\Holdem\a.exe 1000
    (0)
    handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
    [main] a 1000 (0) handle_exceptions: Dumping stack trace to a.exe.core

    Here is the code that I think anyone will need to look at to help me solve
    this, and I'll add a comment on the line that I believe is causing the
    error:
    From the CardDeck class:
    int drawCards[12];

    From the Player class:
    int cardsInHand[2];

    // gets the draw cards
    int* CardDeck::getDrawCards()
    {
    // show the cards
    test(drawCards);

    return drawCards;
    }

    void CardDeck::test(int a[])
    {
    for (int i = 0; i < 12; i++)
    cout<<"test card is "<<a<<endl;
    }

    void Player::drawCards(Player players[], CardDeck c)
    {
    int drawCardRank = 0;
    int drawCardSuit = 0;

    // get the cards
    int* cards = c.getDrawCards(); //*NOTE* getDrawCards returns a pointe, but
    i return it drawCards array. is this correct? wouldn't compile otherwise
    cout<<endl;
    //c.test(cards); //*NOTE* if i uncomment this, all 12 cards will display
    without error, so i know the array values are not the problem
    for (int i = 0; i < numberOfPlayers; i++)
    {
    // each player draws one card
    players.cardsInHand = cards; //*NOTE* I believe this is the
    problematic line
    // map the card
    drawCardSuit = players.cardsInHand / 100; // get the actual suit for
    each card
    drawCardRank = players.cardsInHand % 100; // get the actual rank for
    each card
    *NOTE* mapCards simply returns a string for the rank and suit so the
    user will see an actual card instead of an int value
    cout<<players.name<<" draws a "<<c.mapCards(drawCardRank,
    drawCardSuit)<<endl;
    }
    }

    Here is main, in its entirety:
    #include "Holdem.cpp"

    using namespace std;

    int main()
    {
    srand(time(0));
    CardDeck cards;
    // this needs to be a 11 element array - 10 players, one dealer
    Player players[11];
    players[0].initializePlayerArray(players); // all this does is gives access
    to the array of players from main to the functions in the player class
    players[0].drawCards(players, cards);
    return 0;
    }

    Can anyone help me figure this out? My assumption is that I'm just not
    assigning the first element of the cardsInHand array of each player a value
    from the drawCards array properly, but I don't know how else to do it.

    Thanks in advance!!!
    SB, Oct 30, 2003
    #1
    1. Advertising

  2. SB

    Ron Natalie Guest

    "SB" <> wrote in message news:a28ob.122478$a16.6145@lakeread01...
    \
    > for (int i = 0; i < numberOfPlayers; i++)
    > {
    > // each player draws one card
    > players.cardsInHand = cards; //*NOTE* I believe this is the
    > problematic line


    Of course it's the problematic line i numberOfPlayers is greater than 2.
    Just what do you think you are doing using the same index everywhere.
    cardsInHand is only two elements.

    It would appear you want seperate indexes for all three of those arrays.
    Ron Natalie, Oct 30, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sangeetha

    Array of pointer Vs Pointer to Array

    sangeetha, Oct 8, 2004, in forum: C Programming
    Replies:
    9
    Views:
    340
    Tim Rentsch
    Oct 9, 2004
  2. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    666
    Martin Ambuhl
    Jan 28, 2008
  3. RSL
    Replies:
    14
    Views:
    3,224
  4. Hansen
    Replies:
    3
    Views:
    1,088
    rep_movsd
    Apr 24, 2010
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    445
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page