C: accessing members of struct in a function -
i'm trying use struct member 'size' in function print_shoe, loop doesn't run. however, if replace 'c->size' int in loop, runs fine
#include <stdio.h> #include <stdlib.h> #include <string.h> #define deck_size 52 #define num_faces 13 #define num_suits 4 #define length_faces 6 #define length_suits 9 typedef struct cards { char suits[num_suits][length_suits]; char faces[num_faces][num_faces]; int suit, face, card, value, size; int *values[num_faces]; } cards; char buf[101]; void print_shoe(); void init_decks(); int rand_int(); void shuffle(); int main(void) { srand( time(null) ); int decks_input = 0; int numberofdecks = 1; { printf("\nenter number of decks used in game (1-8):\n\n"); if (fgets(buf, sizeof(buf), stdin) != null) if (sscanf (buf, "%d", &decks_input)) numberofdecks = decks_input; } while (numberofdecks < 1 || numberofdecks > 8); cards *shoe = malloc(sizeof(cards) * numberofdecks * deck_size); shoe->size = numberofdecks * deck_size; shuffle(shoe); print_shoe(shoe); free(shoe); return 0; } void print_shoe(cards *c) { int i; (i = 0; < c->size; i++) { printf("card #%d = %s of %s\n", i+1, c->faces[c[i].face], c->suits[c[i].suit]); } } void init_decks(cards *c) { int i; (i = 0; < c->size; i++) { c[i].card = i; c[i].suit = c[i].card % num_suits; c[i].face = c[i].card % num_faces; } } void shuffle(cards *c) { init_decks(c); int i, j; cards tmp; (i = c->size - 1; > 0 ; i--) { j = rand_int(i + 1); tmp = c[j]; c[j] = c[i]; c[i] = tmp; } } int rand_int(int n) { int limit = rand_max - rand_max % n; int rnd; { rnd = rand(); } while (rnd >= limit); return rnd % n; }
edit: question has been updated extensively in response comments needed more clarification
in revised code, have:
cards *shoe = malloc(sizeof(cards) * numberofdecks * deck_size); shoe->size = numberofdecks * deck_size; // need init_decks(shoe); here!!! shuffle(shoe); print_shoe(shoe);
your code in print_shoe()
printing, you've not initialized data malloc()
other size, you're printing garbage. data returned malloc()
uninitialized , must initialized before read. question changed while typing; still haven't called init_decks(shoe);
need to.
this not why isn't working — i'm not sure problem — worth commenting on. have:
void shuffle(cards *c) { init_decks(c); int i, j; cards tmp; (i = c->size - 1; > 0 ; i--) { j = rand_int(i + 1); tmp = c[j]; c[j] = c[i]; c[i] = tmp; } }
if you're going use c99 technique of declaring variables minimal scope, should writing:
void shuffle(cards *c) { init_decks(c); (int = c->size - 1; > 0; i--) { int j = rand_int(i + 1); cards tmp = c[j]; c[j] = c[i]; c[i] = tmp; } }
(i wouldn't have missed call init_decks()
had been written that.)
as noted in comment, cards
structure rather top-heavy. allocate (for each card) enough space store ranks , suits possibly have. not necessary.
this code separates deck
card
. uses flexible array member in deck
structure hold cards, convenient. may prefer use regular pointer there, in case you'll need pair of memory allocations , function deck_free()
release memory allocated deck_alloc()
.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #define num_faces 13 #define num_suits 4 #define deck_size (num_faces * num_suits) #define length_faces 6 #define length_suits 9 static const char suits[num_suits][length_suits] = { "clubs", "diamonds", "hearts", "spades" }; static const char faces[num_faces][num_faces] = { "ace", "deuce", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king", }; typedef struct card { int suit; int face; int card; } card; typedef struct deck { int size; card cards[]; // flexible array member } deck; void print_shoe(const deck *d); void init_decks(deck *d); int rand_int(int n); void shuffle(deck *d); static deck *deck_alloc(int numberofdecks); int main(void) { srand( time(null) ); int numberofdecks = 1; { char buf[101]; printf("\nenter number of decks used in game (1-8):\n\n"); if (fgets(buf, sizeof(buf), stdin) != null) { int decks_input; if (sscanf (buf, "%d", &decks_input)) numberofdecks = decks_input; } } while (numberofdecks < 1 || numberofdecks > 8); deck *shoe = deck_alloc(numberofdecks); shuffle(shoe); print_shoe(shoe); free(shoe); return 0; } static deck *deck_alloc(int numberofdecks) { deck *shoe = malloc(sizeof(deck) + (sizeof(card) * numberofdecks * deck_size)); if (shoe == 0) { fprintf(stderr, "out of memory\n"); exit(1); } shoe->size = numberofdecks * deck_size; return shoe; } void print_shoe(const deck *d) { (int = 0; < d->size; i++) printf("card #%d = %s of %s\n", i+1, faces[d->cards[i].face], suits[d->cards[i].suit]); } void init_decks(deck *d) { (int = 0; < d->size; i++) { d->cards[i].card = i; d->cards[i].suit = d->cards[i].card % num_suits; d->cards[i].face = d->cards[i].card % num_faces; } } void shuffle(deck *d) { init_decks(d); (int = d->size - 1; > 0 ; i--) { int j = rand_int(i + 1); card tmp = d->cards[j]; d->cards[j] = d->cards[i]; d->cards[i] = tmp; } } int rand_int(int n) { int limit = rand_max - rand_max % n; int rnd; { rnd = rand(); } while (rnd >= limit); return rnd % n; }
sample output:
$ ./cards enter number of decks used in game (1-8): 1 card #1 = 8 of clubs card #2 = jack of clubs card #3 = deuce of diamonds card #4 = jack of hearts card #5 = queen of clubs card #6 = 4 of hearts card #7 = 6 of spades card #8 = king of hearts card #9 = 5 of spades card #10 = king of clubs card #11 = deuce of clubs card #12 = king of spades card #13 = 4 of spades card #14 = 9 of diamonds card #15 = 5 of hearts card #16 = deuce of spades card #17 = ten of clubs card #18 = 5 of diamonds card #19 = ten of spades card #20 = 3 of spades card #21 = 9 of hearts card #22 = 6 of clubs card #23 = ace of clubs card #24 = 3 of clubs card #25 = queen of hearts card #26 = jack of diamonds card #27 = 9 of clubs card #28 = 4 of clubs card #29 = 7 of spades card #30 = ace of diamonds card #31 = 6 of diamonds card #32 = 3 of hearts card #33 = queen of diamonds card #34 = ten of hearts card #35 = ten of diamonds card #36 = 7 of diamonds card #37 = 7 of clubs card #38 = deuce of hearts card #39 = ace of hearts card #40 = jack of spades card #41 = 8 of diamonds card #42 = 8 of spades card #43 = ace of spades card #44 = 3 of diamonds card #45 = queen of spades card #46 = 5 of clubs card #47 = 4 of diamonds card #48 = king of diamonds card #49 = 9 of spades card #50 = 8 of hearts card #51 = 6 of hearts card #52 = 7 of hearts $
Comments
Post a Comment