c - c 中 4x4 矩阵的 getter 问题

标签 c arrays pointers return tetris

你好,明天之前我必须用 C 语言编写一个俄罗斯方 block 游戏,但我在使用 getter 时遇到了一些麻烦,该 getter 应该以 4x4 矩阵的形式返回 Sprite 。

这很可能非常简单,尽管我对数组指针不太熟悉。

所以在我存储形状的第一个文件中,这是 1 个形状的测试 getter

int shape_i[4][4] = {
    {1,0,0,0},
    {1,0,0,0},
    {1,0,0,0},
    {1,0,0,0}
};


int **get_shape(){
return shape_i;}

现在从我的绘图文件中我这样调用它

void draw_shape(){
int **shape= get_shape();
for (int i = 0; i<4; i++){
    for (int j=0; j<4; j++){
        int value = shape[j][i];

            if (value != 0){
                SDL_Rect rect;
                rect.x = (get_x()+i)*BLOCK_WIDTH;
                rect.y = (get_y()+j) *BLOCK_HEIGHT;
                rect.h = BLOCK_HEIGHT;
                rect.w = BLOCK_WIDTH;
                SDL_FillRect(window,&rect,0x044DDE);
            }
            }
    }
SDL_Flip(window);
}

编译时没有给出错误,但我的程序一旦到达 get_shape() 就会停止

最佳答案

TL;DR:使用 memcpy 将数据复制到形状数组中,请参见下面的第二个示例。其他示例是带有解释的替代方法。

<小时/>

在 C 中,不能返回数组。您只能将指向数组第一个元素的指针返回。数组的第一个元素本身就是一个数组,一个包含 4 int 的数组。 s。

定义指向四个整数数组的指针的语法有点巴洛克:

int (*p)[4];

当您必须将其定义为函数的返回类型时,这就更加巴洛克了:

int (*get_shape(int c))[4] { ... }

解决这个问题的方法是使用 typedef :

typedef int (*Shape)[4];

现在你的变量和函数原型(prototype)如下所示:

Shape p = get_shape(c);

Shape get_shape(int c) { ... }

这是一个完整的示例:

#include <stdlib.h>
#include <stdio.h>

typedef int (*Shape)[4];

int shape_i[4][4] = {{1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0}};
int shape_l[4][4] = {{1,0,0,0}, {1,0,0,0}, {1,1,0,0}, {0,0,0,0}};
int shape_z[4][4] = {{1,0,0,0}, {1,1,0,0}, {0,1,0,0}, {0,0,0,0}};

Shape get_shape(int c)
{
    switch (c) {
    case 'I':   return shape_i;
    case 'L':   return shape_l;
    case 'Z':   return shape_z;
    }
    return NULL;
}

int main()
{
    int c;

    for (c = 'A'; c <= 'Z'; c++) {
        Shape p = get_shape(c);

        if (p) {
            int i, j;

            for (j = 0; j < 4; j++) {
                for (i = 0; i < 4; i++) {
                    putchar(p[j][i] ? '#' : ' ');
                }
                puts("");
            }
            puts("--");
        }        
    }

    return 0;
}

请注意形状的定义仍然要求您使用 int[4][4] ,因为数组不是指针。您需要用数组来定义数据。另请注意,此解决方案返回指向原始 shape_i 的指针。 。当你修改p中的数据时,您修改shape_i通过p ,从而破坏你的形状原型(prototype)。

<小时/>

如果您想用数据填充数组,只需将数据传入即可。即使对于一维数组,这也是一种常见的方法:传递一个数组并让函数填充它。返回一个(否则不相关的)值,告诉您操作是否成功。

int get_shape(int shape[4][4], int c)
{
    switch (c) {
    case 'I':   memcpy(shape, shape_i, sizeof(shape)); return 1;
    case 'L':   memcpy(shape, shape_l, sizeof(shape)); return 1;
    case 'Z':   memcpy(shape, shape_z, sizeof(shape)); return 1;
    }
    return 0;
}

memcpy是标准库的一个函数,您应该为其添加 <string.h> 。返回值只是检查形状是否有效。像这样使用它:

int p[4][4];

if (get_shape('I')) {
    // p is now filled with a copy of shape_i
}

我认为这是您应该使用的方法。它将复制 shape_t 的内容至p ,这就是您想要的。您将旋转并翻转当前 block p ,而您想保留 block 原型(prototype) shape_i future 的“克隆”不变。

<小时/>

我上面说过,你不能在 C 中返回数组。你能做的就是将数组包装在 struct 中。并返回。结构按每个值传递,不会衰减为像数组那样的指针。

struct Shape {
    int data[4][4];
};

struct shape shape_i = {{{1,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,0}}};

struct Shape get_shape(void) {
    return shape_i;
};

struct Shape p = get_shape();

这也将复制 cntents,但您必须以 p.data[i][j] 的形式访问元素。 .

关于c - c 中 4x4 矩阵的 getter 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34842716/

相关文章:

c - Malloc 字符串并编辑它

c -/* ARGSUSED */等特殊注释

c++ - 访问结构的 union 成员的优雅方式

python - 智能处理具有多个索引的 Python 数组

c++ - 重载下标运算符不返回指针

c - scanf 多个变量

javascript - 具有主值和次值的数组排序

c# - 将完全小写/大写字符数组转换回用户输入

c - 这个指针是否安全声明?

C - 将预定义函数作为指针传递