传递结构的二维数组 [英] Passing a 2D array of structs

查看:25
本文介绍了传递结构的二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从数组中处理卡片类型的结构.

Im dealing out card type structures from an array.

struct card deck[DECKSIZE];  //The deck is an array of cards structures

我使用的是二维数组.卡片类型结构数组的数组

Im using a 2D array. An array of arrays of card type structures

struct card allHands[hands][cards];

我使用这个函数传递甲板和数组作为带有数组参数的指针.我还更改了牌组指针的位置,以模拟在将牌传递给玩家时丢失牌的牌组.

Im using this function passing the deck and the array as pointers with the parameters of the array. I also change the location of the deck pointer to simulate the deck losing the card as it is passed to the player.

void dealHands(struct card *deck, struct card **handArray, int hands, int cards){

    int players;
    int cardCount;
    int passCard = 0;
    struct card * thisDeck;

    thisDeck = deck;

    for(players = 0; players < hands; players++){
        for(cardCount = 0; cardCount < cards; cardCount++){
            handArray[players][cardCount] = thisDeck[passCard];
            passCard++;
        }   
    }
    deck = (deck + passCard);   
}   

我用 c 编程已经很长时间了,所以我想这就是你做原型的方式?

Its been a long while that I program in c so I think this is how you do the prototype?

    void dealHands(struct card[], struct card*[], int, int);

这就像我如何实现函数的主要框架.

This is like the skeleton of my main of how Im implementing the function.

int main(int argc, char *argv[])
{
    /* Declare and initialize variables  */

    int hands = atoi(argv[HANDSINPUT]);  //How many players 
    int cards = atoi(argv[CARDSINPUT]);  //How many cards per hand  
    struct card deck[DECKSIZE];  //The deck is an array of cards structures

    struct card allHands[hands][cards];

    //Builds the deck
    //Shuffles deck with a RNG and swapping every card

    int players;
    int cardCount;
    int passCard = 0;   


    dealHands(deck, allHands, hands, cards);


}

我在编译过程中得到以下 2 条语句

I get the 2 following statements during compilation

警告:从不兼容的指针类型传递‘dealHands’的参数 2 [默认启用]DealHands(甲板,allHands,手,卡片);^

warning: passing argument 2 of ‘dealHands’ from incompatible pointer type [enabled by default] dealHands(deck, allHands, hands, cards); ^

注意:应为struct card **",但参数类型为struct card ()[(sizetype)(cards)]"void dealHands(struct card[], struct card[], int, int);^

note: expected ‘struct card **’ but argument is of type ‘struct card ()[(sizetype)(cards)]’ void dealHands(struct card[], struct card[], int, int); ^

当我需要在函数中调用指针和数组时,我总是搞砸.所以我不确定我的逻辑在哪里有缺陷.我在哪里传递地址而不是值,反之亦然?

I always mess up when it comes to pointers and arrays when I need to call them in a function. So Im not sure where my logic is flawed. Where am I passing the address rather than the value or vice versa?

推荐答案

将数组作为函数参数传递时最重要的事情是:你不能向/从函数传递数组.

The most important thing when passing arrays as function arguments is: You can't pass an array to/from a function.

说了这么多,请仔细阅读以下内容.(为了简单起见,我将使用 int,但当然您也可以使用其他类型.)

Said that, read the following very carefully. (I'll use int for simplicity, but of course you can use other types, too.)

但是您可以传递数组的指向第一个元素的指针".幸运的是,C 会自动进行转换.更重要的是,除了三个例外(sizeof_Alignof& 运算符),C 会自动将数组的名称转换为这样的指针.这通常被称为数组衰减指向第一个元素的指针".

But you can pass a "pointer to the first element" of an array. Luckily, C does the conversion automatically. Even more, for all but three exceptions (sizeof, _Alignof, & operators), C converts the name of an array automatically to such a pointer. This is often called "the array decays to a pointer to the first element".

但是这种衰减不是递归的.所以,如果你将一个二维数组传递给一个函数,它会衰减为一个指向一维数组的指针:

But this decaying is not recursive. So, if you pass a 2D array to a function, it decays to a pointer to a 1D array:

int a[1][2];    // 1 = outer dimension, 2 = inner

当传递给函数时

void f(int a[1][2]);

变成

int (*a)[2]   // pointer to array of inner dimension

或者可以显式使用指针语法

Alternatively one can explicitly use pointer syntax

void f(int (*a)[2]);

a 的类型在所有情况下都是 int (*)[2].注意括号!您使用哪种语法基本上是个人喜好.我更喜欢所有维度的数组语法,因为它更清楚地记录了意图.

The type of a is int (*)[2] for all cases. Mind the parentheses! Which syntax you use is basically personal preference. I do prefer the array syntax with all dimensions, because that documents the intention more clearly.

您始终必须传递所有尺寸,最外层尺寸除外.这仅用于文档,不是必需的(请参见下面的示例).

You always have to pass all sizes, except for the outermost dimension. That is just for documentation and not required (see below for an example).

在函数内部,你使用普通的索引运算符:

Inside the function, you use normal index-operator:

int t, i;    // index variable for ouTer, Inner
a[t][i];

请注意,这可以应用于更高维度的数组.对于一维数组,这实际上也适用.只需删除内部尺寸:

Note this can be applied to higher dimension arrays. For 1D array, this also applies, actually. Just remove the inner dimension:

int a[1];

衰减到

int *a;

(我只是使用常量12 来给维度编号;当然你可以使用你想要的维度.)

(I just used the constants 1 and 2 to number the dimensions; of course you can use the dimensions you want.)

如果你想传递一个可变长度的数组(VLA,_variable length array),你必须将除了最外层的维度之外的所有维度都传递给函数:

If you want to pass an array with variable length (VLA, _variable length array), you have to pass all but the outermost dimension to the function:

f(int inner, a[][inner]);

但对于检查等更好的是通过所有维度:

But better for checking, etc. is to pass all dimensions:

f(int outer, int inner, a[outer][inner]);

这篇关于传递结构的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆