c - error: variable-sized object may not be initialized. Unsure why? -
i attempting fill deck of cards using c (i new c) keep getting error, error: variable-sized object may not initialized line
char **deck[r] = values[v], suits[d], colour[s];
this full code. attempting fill deck of cards , store deck in array deck[52] using colour, suit , value arrays each card in deck. if logic wrong how can put suit , face , colour deck in order fill deck of cards?
#include<stdio.h> #include<stdlib.h> #include<time.h> typedef unsigned char card; static char *suits[] = { "hearts", "diamonds", "clubs", "spades" }; static char *values[] = { "ace", "two", "three", "four", "five", "six", \ "seven", "eight", "nine", "ten", "jack", \ "queen", "king" }; static char *colour[] = { "black", "red" }; void filldeck(card deck[52]); int main() { filldeck(deck); return 0; } void filldeck(card deck[52]) { int r; r = 0; int v; int d; int s; ( v = 0; v < 13; v++) { ( d = 0; d < 4; d++) { ( s = 0; s < 2; s++) { char **deck[r] = values[v], suits[d], colour[s]; printf("%c", deck[r]); r++; } } } return; }
if try
deck[r] = values[v], suits[d], colour[s];
warning: assignment makes integer pointer without cast
p.s. there 52 cards in deck
please help
1) first of you're not working correct data type, imposible char hold information. should define own datatype recommend following.
typedef struct { unsigned int suits; unsigned int value; unsigned int colour; }card_t;
2) recommend storing each card int , using enum distenguish between each value following:
enum suits{hearts=0; diamonds, clubs, spades};//diamonds =1, clubs=2,etc enum values{ ace=1, two=2, (...) ,king=13}; enum colour{black=0,red};
3)you never decleared deck, , passed function. here's fix that
int main(void) { card_t deck[52]; filldeck(deck); return 0; }
4) it's array of pointers, , trying fit 3 pointers 1 place in array
char **deck[r] = values[v], suits[d], colour[s];
thats why need structure
void filldeck(card_t *deck) { int r=0,v,d,s; ( v = 0; v < 13; v++) ( d = 0; d < 4; d++) ( s = 0; s < 2; s++) { deck[r].suits=d; deck[r].value=v; deck[r].colour=s; r++; } }
ps: check loops think you're filling 104 spaces (13*4*2) maybe need bigger deck size since you're using 2 colors
heres whole thing fixed up
#include <stdio.h> #include <stdlib.h> //static char *suits[] = { "hearts", "diamonds", "clubs", "spades" }; //static char *values[] = { "ace", "two", "three", "four", "five", "six", \ //"seven", "eight", "nine", "ten", "jack", \ //"queen", "king" }; //static char *colour[] = { "black", "red" }; enum suits{hearts=0; diamonds, clubs, spades};//diamonds =1, clubs=2,etc enum values{ ace=1, two=2, (...) ,king=13}; enum colour{black=0,red}; typedef struct { unsigned int suits; unsigned int value; unsigned int colour; }card_t; void filldeck(card deck[52]); int main(void) { card_t deck[52];//might 104 filldeck(deck); return 0; } void filldeck(card_t *deck) { int r=0,v,d,s; ( v = 0; v < 13; v++) ( d = 0; d < 4; d++) ( s = 0; s < 2; s++) { deck[r].suits=d; deck[r].value=v; deck[r].colour=s; r++; } }
if want work chars , not numbers(not practical ) define structure following
typedef struct { char * suits; char * value; char * colour; }card_t;
and loop following
void filldeck(card_t *deck) { int r=0,v,d,s; ( v = 0; v < 13; v++) ( d = 0; d < 4; d++) ( s = 0; s < 2; s++) { deck[r].suits=suits[d]; deck[r].value=values[v]; deck[r].colour=colour[s]; r++; } }
Comments
Post a Comment