Home>

Developing bubbling without C language.
The following error occurred in the process (CreateHand) of creating a group (deck) of 53 cards and distributing it to the three players in sequence.

I want to pass the deck created by CreateDeck function to CreateHand function and generate player_cards array.

Error message
Main.c: 22: 12: warning: incompatible pointer to integer conversion returning 'char [53] [4]' from a function with result type 'char' [-Wint-conversion]
    return deck;
           ^ ~~~
Main.c: 26: 17: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
char CreateHand (array []) {
                ^
Main.c: 34: 9: warning: implicitly declaring library function 'strcpy' with type 'char * (char *, const char *)' [-Wimplicit-function-declaration]
        strcpy (player_cards [player_number] [j], array [i]);
        ^
Main.c: 34: 9: note: include the header<string.h>or explicitly provide a declaration for 'strcpy'
Main.c: 34: 47: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Wint-conversion]
        strcpy (player_cards [player_number] [j], array [i]);
                                              ^ ~~~~~~~
Main.c: 41: 1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
Main.c: 47: 16: warning: incompatible integer to pointer conversion passing 'char' to parameter of type 'int *' [-Wint-conversion]
    CreateHand (deck1);
               ^ ~~~~
Main.c: 26: 17: note: passing argument to parameter 'array' here
char CreateHand (array []) {
Applicable source code
char CreateDeck () {
// Create a deck
    char deck [53] [4] = {"S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "S10", "S11", "S12", "S13",
        "H1", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "H10", "H11", "H12", "H13 ",
        "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10", "C11", "C12", "C13 ",
        "D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "D11", "D12", "D13 "," J0 "
    };
    return deck;
}
char CreateHand (array []) {
// 3 Generate hand for every person
// 3 array used as a hand for 3 people
    char player_cards [3] [18] [4];
    int player_number = 0;
    int j = 0;
    for (int i = 0;i<53;i ++) {
        // Distribute cards in the order of A, B, C, A, B, C until there are no more cards in the deck
        strcpy (player_cards [player_number] [j], array [i]);
        player_number ++;
        if (player_number>= 3) {
            player_number = 0;
            j ++;
            }
        }
}
int main (void) {
    char deck1 = CreateDeck ();
    CreateHand (deck1);
}
Tried If i write all the code in the main function and execute it without using the

function, it worked.

Supplemental information (FW/tool version etc.)

I'm running the code with paiza.io.

c
  • Answer # 1

    Some bad things.

      

    char CreateDeck () {

    There is no argument definition. Enter void if there are no arguments

      

    return deck;

    Return value should be char, but returns char **

      

    char CreateHand (array []) {

    no array type<<< second warning
    Also, there is no return value for this function

    Do not write code in an atmosphere


    If you are serious about writing the first array,

    typedef char * CARD_DECK [4] [13];
    CARD_DECK CreateDeck () {
        static CARD_DECK deck = {
            {"S1", ...},
            {"H1", ...},
            {"C1", ...},
            {"D1", ...},
        };
        return deck;
    }

    I ’m sure you ’ll become Yufufu
    #Oh, let's do the joker

  • Answer # 2

    Although it looks a bit, char deck [53] [4] is an auto variable (intra function variable) Therefore, it becomes invalid after exiting the function. So if you don't set it to static or static const, it will disappear.

  • Answer # 3

    A few things you noticed.

      

    char deck [53] [4]

    Since it is a local variable, it disappears when it exits the function.

      

    char CreateDeck () {
      return deck;

    The variable type and return type do not match.

      

    char CreateHand (array []) {

    The argument type is undefined.
    Although the return type is defined, it is not returning.