数组作为函数的参数 [英] array as parameter of a function

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

问题描述

有是结构的阵列。

    static field fields[xsize][ysize];

我想改变它在功能上

I want to change it in function

    void MoveLeft(pacman *Pacman, field **fields,int **play)

但是,当我把它像这样

But when I send it like this

     MoveLeft(&Pacman,fields,play);

我有一个错误。

场 - 结构

     typedef struct
     {
    blossom blossoms;
    wall walls;
     }field;

在这里开花和放大器;墙 - 另一种结构

where blossom & wall - another structures

推荐答案

虽然数组和指针在C有点互换,他们是不完全一样的。特别是,数组的数组和指针数组在内存中不同的版面。

Although arrays and pointers are somewhat interchangeable in C, they're not exactly the same. In particular, an array of arrays and an array of pointers are laid out differently in memory.

下面是一个方法,使指针数组是指相同的数据作为您现有的数组的数组:

Here's a way to make an array of pointers which refers to the same data as your existing array of arrays:

field* field_rows[xsize];
for (unsigned int i=0; i<xsize; i++) {
    field_rows[i] = fields[i];
}

然后一个指向 field_rows 数组可以传递到 MoveLeft

MoveLeft(&Pacman,field_rows,play);

另一种解决方案可能是改变 MoveLeft 的声明相反,采取一个指向数组的数组:

Another solution might be to change the declaration of MoveLeft instead, to take a pointer to array of arrays:

void MoveLeft(pacman *Pacman, field fields[xsize][ysize], int **play);

MoveLeft(&Pacman,fields,play);

这篇关于数组作为函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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