-1

I am attempting to fill a deck of cards using c (I am new to c) however I keep getting the error, error: variable-sized object may not be initialized for the line

char **deck[r] = values[v], suits[d], colour[s];     

This is the full code. I am attempting to fill a deck of cards and store the deck in the array deck[52] using the colour, suit and value arrays to get each card in the deck. If my logic is wrong then how can I put the suit , face and colour into the deck in order to fill a 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;

    for ( v = 0; v < 13; v++)
    {
        for ( d = 0; d < 4; d++)
        {
            for ( s = 0; s < 2; s++)
            {
                char **deck[r] = values[v], suits[d], colour[s];
                printf("%c", deck[r]);
                r++;
            }
        }
    }

    return;
}

If I try

deck[r] =  values[v], suits[d], colour[s];

warning: assignment makes integer from pointer without a cast

p.s. there are 52 cards in the deck

Please help

  • 5
    Duplicate: [C compile error: “Variable-sized object may not be initialized”](https://stackoverflow.com/questions/3082914/c-compile-error-variable-sized-object-may-not-be-initialized). And your intializer is wrong anyway for two reasons. The syntax is wrong as array initializers needs to be enclosed in `{ }` (exception for string literal initializers). And the types are incompatible as `values[v]` etc are `char *` not `char **`. – kaylum Mar 07 '16 at 02:20
  • @kaylum not a duplicate: although it is the same error message, OP is not trying to initialize a variable-sized object – M.M Mar 07 '16 at 04:07
  • I was puzzled as to how OP expected to store an array of values, suits, and colours, all within a single `unsigned char`... then I saw the username – M.M Mar 07 '16 at 04:09
  • @M.M You are right. The code as shown is trying to initialize a VLA. But a more thoughtful reading of the question does indeed point to that not being what the OP is really intending to do in the context of the whole program. Duplicate close retracted. – kaylum Mar 07 '16 at 04:12
  • I think you need to start with the basics on C. Here is a SO approved list of C books: http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – RedX Mar 07 '16 at 05:33

1 Answers1

2

1) First of all you're not working with the correct data type, its imposible for a char to hold that information. You should define your own datatype I recommend as following.

typedef struct 
{
    unsigned int suits;
    unsigned int value;
    unsigned int colour;

}card_t;

2) I recommend storing each card with an int and using enum to distenguish between each value as 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 the deck, and you passed it to function. Here's a fix for that

int main(void)
{
    card_t deck[52];
    filldeck(deck);

    return 0;
}

4) It's an array of pointers, and your trying to fit three pointers into one place in the array

char **deck[r] = values[v], suits[d], colour[s];

Thats why you need a structure

void filldeck(card_t *deck)
{
    int r=0,v,d,s;

    for ( v = 0; v < 13; v++)
        for ( d = 0; d < 4; d++)
            for ( s = 0; s < 2; s++)
            {
                deck[r].suits=d;
                deck[r].value=v;
                deck[r].colour=s;
                r++;
             }

}

Ps: check your loops I think you're filling 104 spaces (13*4*2) maybe you need a bigger deck size since you're using two colors

Heres the 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 be 104
    filldeck(deck);

    return 0;
}

void filldeck(card_t *deck)
{
    int r=0,v,d,s;

    for ( v = 0; v < 13; v++)
        for ( d = 0; d < 4; d++)
            for ( s = 0; s < 2; s++)
            {
                deck[r].suits=d;
                deck[r].value=v;
                deck[r].colour=s;
                r++;
            }

}

If you want to work with chars and not numbers(not really practical ) define the structure as following

typedef struct 
{
    char * suits;
    char * value;
    char * colour;

}Card_t;

And the loop as following

void filldeck(card_t *deck)
{
    int r=0,v,d,s;

    for ( v = 0; v < 13; v++)
        for ( d = 0; d < 4; d++)
            for ( s = 0; s < 2; s++)
            {
                deck[r].suits=suits[d];
                deck[r].value=values[v];
                deck[r].colour=colour[s];
                r++;
            }

}
Mr. Branch
  • 442
  • 2
  • 13