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

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

问题描述

我可是从一个阵列处理出牌型结构。

 结构张牌[DECKSIZE] //桥面卡结构的数组

即时通讯使用一个二维数组。卡类型结构数组的数组

 结构卡allHands [双手] [卡]。

林使用此函数传递甲板和阵列与阵列的参数的指针。我还改变甲板指针的位置来模拟甲板,因为它被传递给玩家失去该卡。

 无效dealHands(结构卡*甲板,结构卡** handArray,双手诠释,诠释卡){    诠释玩家;
    INT cardCount;
    INT密码卡片= 0;
    结构卡* thisDeck;    thisDeck =甲板;    对于(玩家= 0;播放器和LT;手;玩家++){
        为(cardCount = 0; cardCount&下;卡; cardCount ++){
            handArray [玩家] [cardCount] = thisDeck [密码卡片]
            密码卡片++;
        }
    }
    甲板=(甲板+密码卡片);
}

它是一个漫长的,而在C,我的程序,所以我觉得这是你该怎么办原型?

 无效dealHands(结构卡[],结构卡* [],INT,INT);

这是我喜欢的如何进出口实现main函数的骨架。

  INT主(INT ARGC,CHAR *的argv [])
{
    / *声明并初始化变量* /    诠释手=的atoi(argv的[HANDSINPUT]); //多少球员
    INT卡=的atoi(argv的[CARDSINPUT]);每手//多少卡
    结构张牌[DECKSIZE] //桥面卡结构的数组    结构卡allHands [双手] [卡]。    //构建甲板
    //搅乱甲板有RNG和交换每一张牌    诠释玩家;
    INT cardCount;
    INT密码卡片= 0;
    dealHands(甲板,allHands,手,卡);
}

我得到编译过程中的2个以下语句

警告:传递从兼容的指针类型'dealHands的论据2 [默认启用]
  dealHands(甲板,allHands,手,卡);
  ^

请注意:预期'结构卡**,但参数的类型为结构卡()(sizetype)(卡)
 无效dealHands(结构卡[],结构卡
的[],INT,INT);
      ^

我当谈到指针和数组,当我需要给他们打电话的功能总是陷入困境。因此,林不知道在我的逻辑是有缺陷的。我在哪里传递地址,而不是价值,反之亦然?


解决方案

最重要的传递数组作为函数参数的时候是:您不能从一个函数传递一个数组/ <。 / p>

说,读得很仔细以下。 (我会使用 INT 为简单起见,当然也可以使用其他类型的了。)

但你可以传递一个指针的第一个元素的数组。幸运的是,C自动进行转换。更有甚者,为所有,但三个例外(的sizeof _Alignof &安培; 运营商),C自动转换数组名这样的指针。这通常被称为所述阵列的衰减的的指针的第一个元素

但是,这衰减不是递归。所以,如果你传递一个二维数组给一个函数,它衰变的指针一维数组:

  int类型的[1] [2]; // 1 =外部尺寸,2 =内

当传递给一个函数

 无效F(int类型的[1] [2]);

变为

  INT(*一)[2] //指向内部尺寸的阵列

另外可以明确地使用指针语法

 无效F(INT(*一)[2]);

类型 A INT(*)[2] 所有案件。当心括号!您使用的语法基本上是个人的preference。我做preFER所有维数组语法,因为这更清楚地记录了意向。

您总是要通过各种规模,除了最外面的维度。这仅仅是文件和不需要(见下面的例子)。

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

  INT T,I; //外,内标变量
一[T] [I]

请注意这可以适用于更高维阵列。对于一维数组,这也适用,实际上。只是删除内部尺寸:

  int类型的[1];

衰减到

 为int *一个;

(我只是用常量 1 2 来编号的尺寸;当然你可以用维你想要的。)


如果你想通过与可变长度(VLA,_variable长数组)的数组,你必须通过所有,但最外层的维度功能:

  F(INT内,一个[] [内心]);

但检查等,更好的是通过所有维:

  F(INT外,诠释内心,一个[外] [内心]);

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);   
}   

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);


}

I get the 2 following statements during compilation

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

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.

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

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

when passed to a function

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

becomes

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

Alternatively one can explicitly use pointer syntax

void f(int (*a)[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];

decays to

int *a;

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


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天全站免登陆