[SOLVED] [C] Blackjack game - array has 0s in first 24 elements immediately after initialising with values from 1 to 13

EDIT: Literally 1 second after I pressed the post button I got an idea and thats what it was. Why does that always happen? xD

The solution was that my player_hand[] and dealer_hand[] arrays had a size of DECK_SIZE / 2, but I was initialising them to 0 from within the for loop where I was initialising the deck[] array (which has a size of DECK_SIZE), so the last 24 0s of one of those where overflowing into deck[]…

Hello everyone!

I am trying to make a simple blackjack (21) game in C, to practice some pointer stuff, and I am running into an issue with the function that initialises all my arrays. The way I am going about setting everything up, I have an integer array for a standard deck of 52 cards (values from 1 to 13, each 4 times), an integer array for the shuffled deck, and 2 more arrays for the player and dealer hands (irrelevant at this point as they are not used yet).

My issue is that after successfully initialising the deck array (the non-shuffled one) and outputting the values from within the for loop that sets them, I get what I expect to see which is:

1, 2, 3, …, 13, 1, 2, 3, …, 13, … (so on for 52 cards).

But when I output each value from a different for loop in the same function, the first 24 elements (0-23) are 0s each time I run it. I can not figure out why that would be. I ran my program through valgrind which reported that there were no memory leaks.

Here is the relevant code:


<span style="font-weight:bold;color:#a71d5d;">#define </span><span style="color:#323232;">DECK_SIZE </span><span style="color:#0086b3;">52
</span><span style="color:#323232;">
</span><span style="font-weight:bold;color:#a71d5d;">int </span><span style="font-weight:bold;color:#795da3;">initialise_decks</span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">int* </span><span style="color:#323232;">deck, </span><span style="font-weight:bold;color:#a71d5d;">int* </span><span style="color:#323232;">shuffled_deck, </span><span style="font-weight:bold;color:#a71d5d;">int* </span><span style="color:#323232;">player_hand, </span><span style="font-weight:bold;color:#a71d5d;">int* </span><span style="color:#323232;">dealer_hand, </span><span style="font-weight:bold;color:#a71d5d;">int </span><span style="color:#323232;">size)
</span><span style="color:#323232;">{
</span><span style="color:#323232;">	</span><span style="color:#62a35c;">printf</span><span style="color:#323232;">(</span><span style="color:#183691;">"=================INITIALISING==================</span><span style="color:#0086b3;">n</span><span style="color:#183691;">"</span><span style="color:#323232;">);
</span><span style="color:#323232;">	</span><span style="font-weight:bold;color:#a71d5d;">for </span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">int</span><span style="color:#323232;"> i </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">0</span><span style="color:#323232;">; i </span><span style="font-weight:bold;color:#a71d5d;"><</span><span style="color:#323232;"> size; i</span><span style="font-weight:bold;color:#a71d5d;">++</span><span style="color:#323232;">)
</span><span style="color:#323232;">	{
</span><span style="color:#323232;">		deck[i] </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#323232;">(i </span><span style="font-weight:bold;color:#a71d5d;">% </span><span style="color:#0086b3;">13</span><span style="color:#323232;">) </span><span style="font-weight:bold;color:#a71d5d;">+ </span><span style="color:#0086b3;">1</span><span style="color:#323232;">;
</span><span style="color:#323232;">		player_hand[i] </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">0</span><span style="color:#323232;">;
</span><span style="color:#323232;">		dealer_hand[i] </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">0</span><span style="color:#323232;">;
</span><span style="color:#323232;">
</span><span style="color:#323232;">		</span><span style="color:#62a35c;">printf</span><span style="color:#323232;">(</span><span style="color:#183691;">"deck[</span><span style="color:#0086b3;">%d</span><span style="color:#183691;">]: </span><span style="color:#0086b3;">%dn</span><span style="color:#183691;">"</span><span style="color:#323232;">, i, deck[i]);
</span><span style="color:#323232;">	}
</span><span style="color:#323232;">
</span><span style="color:#323232;">	</span><span style="color:#62a35c;">printf</span><span style="color:#323232;">(</span><span style="color:#183691;">"+++++++++++++++++++++++++++++++++++++++</span><span style="color:#0086b3;">n</span><span style="color:#183691;">"</span><span style="color:#323232;">);
</span><span style="color:#323232;">	</span><span style="font-weight:bold;color:#a71d5d;">for </span><span style="color:#323232;">(</span><span style="font-weight:bold;color:#a71d5d;">int</span><span style="color:#323232;"> i </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#0086b3;">0</span><span style="color:#323232;">; i </span><span style="font-weight:bold;color:#a71d5d;"><</span><span style="color:#323232;"> size; i</span><span style="font-weight:bold;color:#a71d5d;">++</span><span style="color:#323232;">)
</span><span style="color:#323232;">	{
</span><span style="color:#323232;">		</span><span style="color:#62a35c;">printf</span><span style="color:#323232;">(</span><span style="color:#183691;">"deck[</span><span style="color:#0086b3;">%d</span><span style="color:#183691;">]: </span><span style="color:#0086b3;">%dn</span><span style="color:#183691;">"</span><span style="color:#323232;">, i, deck[i]);
</span><span style="color:#323232;">	}
</span><span style="color:#323232;">	</span><span style="color:#62a35c;">printf</span><span style="color:#323232;">(</span><span style="color:#183691;">"+++++++++++++++++++++++++++++++++++++++</span><span style="color:#0086b3;">n</span><span style="color:#183691;">"</span><span style="color:#323232;">);
</span><span style="color:#323232;">
</span><span style="color:#323232;">
</span><span style="color:#323232;">	</span><span style="font-weight:bold;color:#a71d5d;">return</span><span style="color:#323232;"> INITIALISE_DECKS_SUCCESS;
</span><span style="color:#323232;">}
</span><span style="color:#323232;">
</span><span style="font-weight:bold;color:#a71d5d;">int </span><span style="font-weight:bold;color:#795da3;">main</span><span style="color:#323232;">() {
</span><span style="color:#323232;">	</span><span style="color:#62a35c;">srand</span><span style="color:#323232;">(</span><span style="color:#62a35c;">time</span><span style="color:#323232;">(</span><span style="color:#0086b3;">0</span><span style="color:#323232;">));
</span><span style="color:#323232;">
</span><span style="color:#323232;">	</span><span style="font-weight:bold;color:#a71d5d;">int</span><span style="color:#323232;"> deck[DECK_SIZE];
</span><span style="color:#323232;">	</span><span style="font-weight:bold;color:#a71d5d;">int</span><span style="color:#323232;"> shuffled_deck[DECK_SIZE];
</span><span style="color:#323232;">	</span><span style="font-weight:bold;color:#a71d5d;">int</span><span style="color:#323232;"> player_hand[DECK_SIZE </span><span style="font-weight:bold;color:#a71d5d;">/ </span><span style="color:#0086b3;">2</span><span style="color:#323232;">];
</span><span style="color:#323232;">	</span><span style="font-weight:bold;color:#a71d5d;">int</span><span style="color:#323232;"> dealer_hand[DECK_SIZE </span><span style="font-weight:bold;color:#a71d5d;">/ </span><span style="color:#0086b3;">2</span><span style="color:#323232;">];
</span><span style="color:#323232;">
</span><span style="color:#323232;">	</span><span style="font-weight:bold;color:#a71d5d;">int</span><span style="color:#323232;"> initialise_status </span><span style="font-weight:bold;color:#a71d5d;">= </span><span style="color:#323232;">initialise_decks(deck, shuffled_deck, player_hand, dealer_hand, DECK_SIZE);
</span><span style="color:#323232;">	</span><span style="font-weight:bold;color:#a71d5d;">if </span><span style="color:#323232;">(initialise_status </span><span style="font-weight:bold;color:#a71d5d;">!=</span><span style="color:#323232;"> INITIALISE_DECKS_SUCCESS)
</span><span style="color:#323232;">	{
</span><span style="color:#323232;">		</span><span style="font-weight:bold;color:#a71d5d;">return</span><span style="color:#323232;"> INITILIASE_DECKS_FAIL;
</span><span style="color:#323232;">	}
</span><span style="color:#323232;">
</span><span style="color:#323232;">	</span><span style="font-weight:bold;color:#a71d5d;">return</span><span style="color:#323232;"> PROCESS_EXIT_NORMAL;
</span><span style="color:#323232;">}
</span>

If you need to try it out for yourself here is the link to the github repo

Chewt,

I actually recently decided to make a CLI blackjack game as well! It was a fun exercise, although my focus was more on the visuals since I enjoy making ascii graphics.

github.com/Chewt/cardterm

Excrubulent,
@Excrubulent@slrpnk.net avatar

Literally 1 second after I pressed the post button I got an idea and thats what it was. Why does that always happen? xD

You were rubber ducking.

promitheas,

Yeap, it seems so. Next time ill try using a friend for my rubber duck 😄

Excrubulent,
@Excrubulent@slrpnk.net avatar

Nah, there’s no shame in it, there’s a reason it’s a phenomenon with a name. At least you posted the solution so now your question might help someone else with a similar problem.

al4s,

player_hand and dealer_hand are only [DECK_SIZE/2] in length, but in initialize_decks you write zeros into them unti [DECK_SIZE -1]. Since the arrays are located next to each other in memory you end up overwriting the deck array.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • programming@programming.dev
  • DreamBathrooms
  • mdbf
  • ethstaker
  • magazineikmin
  • GTA5RPClips
  • rosin
  • thenastyranch
  • Youngstown
  • InstantRegret
  • slotface
  • osvaldo12
  • kavyap
  • khanakhh
  • Durango
  • megavids
  • everett
  • cisconetworking
  • normalnudes
  • tester
  • ngwrru68w68
  • cubers
  • modclub
  • tacticalgear
  • provamag3
  • Leos
  • anitta
  • JUstTest
  • lostlight
  • All magazines