Home>

Developing bubbling without C language.
The following error occurred when executing the process to generate an array of 52 cards.

Error message

function cannot return array type'CARD_DECK'(aka'char * [13] [4]')

Applicable source code
typedef char * CARD_DECK [13] [4];
CARD_DECK CreateDeck (void) {
    static CARD_DECK deck = {
        {"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 "},
    };
    return deck;
}
int main (void) {
   CreateDeck ();
}

typedef char * CARD_DECK [13] [4];
I tried with typedef char * CARD_DECK [4] [13] [4];but I got the same error.

Supplemental information (FW/tool version etc.)

paiza.io

c
  • Answer # 1

    TheCreateDeck ()function itself isn't necessary in this case? (This is as cateye responds.) But if you really want to use this function As a supplement to, you can also: This is not to say that it is better.

    typedef char const * CARD_DECK [4] [13];//<-Note: Changed (see below).
    CARD_DECK * CreateDeck (void) {
        static CARD_DECK deck = {
            {"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 "},
        };
        return&deck;
    }

    Note that the return type is a pointer type. Correspondingly,returnalso returns an address.

    What's wrong with the original code is that the function cannot return an array. However, you can return a "pointer to an array". Returning a local variable pointer is usually a rule, but in this example, the local variabledeckis declared asstatic, so the entity will not be There is no problem because it remains properly.

    However, in that case, you get a pointer, so you must dereference*before using it as an array.

    CARD_DECK * c = CreateDeck ();
    (* c) [1] [3];// "H4"

    Bonus

    typedef, but as you can see from the initializer, you want to create four "char *13 arrays". In the table where the value ofS [i] [j]is written in i rows and j columns, I think that I wanted to create the following table with 4 rows and 13 columns.

    0 1 2 3 4 5 6 7 8 9 10 11 12
    0 S1 S2 S3 S4 S5 S6 S7 S8 S9 S10 S11 S12 S13
    1 H1 H2 H3 H4 H5 H6 H7 H8 H9 H10 H11 H12 H13
    2 C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C13
    3 D1 D2 D3 D4 D5 D6 D7 D8 D9 D10 D11 D12 D13

    This table is represented bytypedef char * CARD_DECK [4] [13].

    By the way, the code written here istypedef char * CARD_DECK [13] [4]with the number of elements reversed, andchar *4 arrays "Is 13 pieces. In a table withS [i] [j]values ​​written in i rows and j columns, 13 rows x 4 columns,

    0 1 2 3
    0 S1 S2 S3 S4
    1 S5 S6 S7 S8
    2 S9 S10 S11 S12
    3 S13 H1 H2 H3
    4 H4 H5 H6 H7
    : : : : :

    (Omitted)

    has been created. I don't mind if it's intentional, but it's not practical ...

    The order around this in a two-dimensional array is a little complicated. You might want to rememberS [number of rows] [number of columns]when you imagine a table.

    In other words, the stored"S1"or"S2"are "string literals" Don't. In order to convey the intention accurately, I think that it is better to use const and make ittypedef const char * CARD_DECK [4] [13].


    (for comments)

    return&deck;means "return the address of the variabledeck". On the other hand,return deck;means "return the address of the first element of the arraydeck". This is because the C language has a rule that "(basically) an array is automatically converted to an address to its first element". It isdeck ==&deck [0]

    Now, these are usually the same as the "address value itself".
    If you remember a lot, int or whatever, usually "address of a variable" refers to the start address of the memory area occupied by that variable. That is,&deckrepresents the start address of the memory area occupied by thedeckarray. On the other hand,deckis&deck [0], so this is calleddeck [0](which is also an array here) Represents the start address of the occupied memory area. If you stare at this calmly, the two will have the same address.

    So what is the difference between them? The answer is "type". You can write down the type, but in the C language, the type is very difficult to understand, so just give it a little alias. In the first place, 2D arrays in C language are "arrays of arrays", so you should be able to name them step by step.

    typedef const char * CARDS [13];// CARDS is an array of 13 strings
    typedef CARDS CARD_DECK [4];// CARD_DECK is an array of 4 CARDS
    // In other words, CARD_DECK represents 4 sets of 13 strings (cards)

    CARD_DECKdefined in this way is the same asCARD_DECKwhentypedef char * CARD_DECK [4] [13]Become.

    Then&deckis a pointer of typeCARD_DECKofdeck, so it will beCARD_DECK *. On the other hand,deckis&deck [0], a pointer of typeCARDSofdeck [0]CARDS *Do you see a difference in one level? A pointer to the array itself and a pointer to the start address of the array. In this way, it is a two-dimensional array, but the law is the same as a one-dimensional one.

    kazura_utb's answer isreturn deck;, but the return value of the function seems to be difficult. kazura_utb'sCreateDeck ()function looks like this using the previous alias: This may be easier to understand.

    CARDS * CreateDeck (void) {...}

    The feeling around here is difficult. I hope you will grab it somehow.

  • Answer # 2

    I think this is a copy of y_waiwai from the previous question,
    As it was said in the answer, there is no joka even though it is bubbling.
    How about settingtypedef char * CARD_DECK [53]first?
    I think that it is better to gradually improve from a simple form.

  • Answer # 3

    It's not the answer ...
    First, I don't understand the need for CreateDeck ().

    static char * deck [] [13] = {
        {"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 "},
    };


    So why not?
    "Appendix"
    usr ~/test/c% ./a.out
    S1
    H2
    C3
    D4
    S5
    H6
    C7
    D8
    S9
    H10

    usr ~/test/c% cat deck.c
    #include<stdio.h>
    #include<string.h>
    static char * deck [] [13] = {
        {"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 "},
    };
    int main (void)
    {
        for (int i = 0;i<10;i ++) {
            puts (deck [i% 4] [i]);
        }
        return 0;
    }
    usr ~/test/c%


    Is it an unintended use?

  • Answer # 4

    If you really want to make a two-dimensional array a return value, write it yourself.
    It has been built and confirmed to work.

    # include<stdio.h>
    char * (* CreateDeck (void)) [13]
    {
        static char * deck [4] [13] = {
            {"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 "},
        };
        return deck;// Since it is an array, it is treated as a pointer, so&is not added. Or&deck [0]
    }
    int main (void) {
        char * (* deck) [13];
        int i, j;
        deck = CreateDeck ();
        for (i = 0;i<4;i ++)
        {
            for (j = 0;j<13;j ++)
            {
                printf ("% s", deck [i] [j]);
            }
            printf ("\ n");
        }
        return 0;
    }

    Since it will be complicated code like this, for example, I think that it is simpler and easier to understand by defining the following structure. (Setting the value is a bit cumbersome but ^^;)

    typedef struct
    {
          char deck [4] [13] [3];
    } CARD_DECK_INFO;

  • Answer # 5

    I ’m sorry
    I misunderstood the language
    Edit your answer

    An array cannot be returned
    Let's return the first address of the array