0

im trying to do a blackjack game for a school project, in C. When i do the "Deal_player" funtion it brings the warning 'passing argumente from incompatible pointer type the warning appears on line 68, i dont know what the problem is. Can someone help please?

void InitEverything(int , int , TTF_Font **, SDL_Surface **, SDL_Window ** , SDL_Renderer ** );
void InitSDL();
void InitFont();
SDL_Window* CreateWindow(int , int );
SDL_Renderer* CreateRenderer(int , int , SDL_Window *);
int RenderText(int , int , const char* , TTF_Font *, SDL_Color *, SDL_Renderer * );
int RenderLogo(int , int , SDL_Surface *, SDL_Renderer * );
void RenderTable(int [], TTF_Font *, SDL_Surface **, SDL_Renderer * );
void RenderCard(int , int , int , SDL_Surface **, SDL_Renderer * );
void RenderHouseCards(int [], int , SDL_Surface **, SDL_Renderer * );
void RenderPlayerCards(int [][MAX_CARD_HAND], int [], SDL_Surface **, SDL_Renderer * );
void LoadCards(SDL_Surface **);
void UnLoadCards(SDL_Surface **);
void baralhar(SDL_Surface **);
void Deal_player(int**);


    int main( int argc, char* args[] )
    {
        SDL_Window *window = NULL;
        SDL_Renderer *renderer = NULL;
        TTF_Font *serif = NULL;
        SDL_Surface *cards[MAX_DECK_SIZE+1], *imgs[2];
        SDL_Event event;
        int delay = 300;
        int quit = 0;
        int money[MAX_PLAYERS] = {110, 110, 110, 110};
        int player_cards[MAX_PLAYERS][MAX_CARD_HAND] = {{0}};
        int house_cards[MAX_CARD_HAND] = {0};
        int pos_house_hand = 0;
        int pos_player_hand[MAX_PLAYERS] = {0};

        // initialize graphics
        InitEverything(WIDTH_WINDOW, HEIGHT_WINDOW, &serif, imgs, &window, &renderer);
        // loads the cards images
        LoadCards(cards);
        baralhar(cards);
        Deal_player(player_cards); //warning


//deal funtion;


void Deal_player(int **player_card){
    int j, h, k = 0;

    for(j=0; j<MAX_PLAYERS; j++){
        for(h=0; h<MAX_CARD_HAND; h++){
            player_card[j][h] = k;printf("jjjjjjjj\n");
            k++;
        }
    }
    for(j=1; j<=4; j++)
        for(h=0; h<2; h++)
            printf("-----%d\n" ,player_card[j][h]);
    return;
}
MDB983
  • 2,444
  • 17
  • 20

2 Answers2

2

The parameter of Deal_player has the wrong type. It should be declared as:

void Deal_player(int (*)[MAX_CARD_HAND]);

And defined as:

void Deal_player(int (*player_card)[MAX_CARD_HAND])
{
    /* ... */
}

Alternatively, you can declare it as:

void Deal_player(int [][MAX_CARD_HAND]);

and define it as:

void Deal_player(int player_card[][MAX_CARD_HAND])
{
    /* ... */
}

Although the alternative looks like the parameter is a 2D array, the compiler will automatically change the type of the parameter to be a pointer to a 1D array.

Ian Abbott
  • 15,083
  • 19
  • 33
0

The problem lies in the difference between a int [][] type and int ** type. People tends to confuse between these two. But array types are different from pointer types, although they might sometime overlap in meaning.

For example for int a[MAX_SOMETHING] type, sizeof(a)/sizeof(a[0]) calculates in compile-time the amount of elements in our a array. But forint *a = malloc(MAX_SOMETHING*sizeof(int)) it yields a different unexpected result (since malloc is made in runtime and sizeof is executed in compile time).

I'm pretty sure there are much more examples that expressing the difference between these two, but this one just catchy.

Instead send as a parameter a pointer to your array of arrays and change the signature of your callee as follows:

//caller - take the address
deal_players(&player_cards);    
//callee
void deal_players_ptr_to_arr(int (*arr)[MAX_PLAYERS][MAX_CARDS])

note 1: if you are confused with the (*arr) part int (*arr)[MAX_PLAYERS][MAX_CARDS] is read as pointer to array of size MAX_PLAYERS of arrays of size MAX_CARDS.

note 2: you can typedef that long type name in some nice players_cards_t name, but it isn't recommended.

Community
  • 1
  • 1
Aviv
  • 414
  • 4
  • 16