Passing 2D Array of Pointers in C

后端 未结 4 1943
忘了有多久
忘了有多久 2021-01-23 09:47

For my program I need to pass a 2D array of pointers to a function in a separate file. I\'ve written a similarly-syntaxed file below:

#include 
#         


        
4条回答
  •  星月不相逢
    2021-01-23 10:33

    if you pass an array to a function, you have to specify the size of the inner array, in your case, instead of void setFirst(card_t *cards[][]), you should specify void setFirst(card_t *cards[][5]).

    Why do you need to specify it and not the size of the first dimension?

    Since cards is an array of array of card_t pointers, if you want to get to cards[1][0], the compiler will need to know how much to add to the pointer cards - cards is declared: card_t *cards[5][4] it will need to add 4 * sizeof(*card_t) to get to cards[1][0], but if cards is declared: card_t *cards[5][5] it will need to add 5 * sizeof(*card_t) to get to cards[1][0].

提交回复
热议问题