Passing 2D Array of Pointers in C

后端 未结 4 1940
忘了有多久
忘了有多久 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:37

    As has been mentioned, to pass a 2d array to a function, you need to have every dimension but the first declared. However, you can also just pass the pointer, as follows. Note that you should always (unless the array dimension is completely fixed and the function that operates on the array only operates within the array's dimension) pass the length of each array, too.

    void setFirst(card_t ***cards, size_t n, size_t m) {
      if (n > 0 && m > 0) {
        cards[0][0]->state = 1;
      }
    }
    

    Because referencing an array via code[0][0] is the same as *(*(code+0)+0*m), you can pass two pointers instead of array dimensions.

提交回复
热议问题