【问题标题】:Initialization of multidimensional pointers with different dimensional anonym arrays inline内联不同维匿名数组的多维指针初始化
【发布时间】:2016-07-13 17:17:59
【问题描述】:

我遇到了菜鸟问题。 我正在用 c 制作俄罗斯方块。 我想为每个实例在结构内联中初始化一个双指针。 数组的宽度不同,但在另一个变量中定义。 代码:

typedef struct {
    char height, width;
    char **shape;
} Shape;

const Shape S_shape = {2,3, (char [][3]){{0,1,1},{1,1,0}}};
const Shape Z_shape = {2,3, (char [][3]){{1,1,0},{0,1,1}}};
const Shape T_shape = {2,3, (char [][3]){{0,1,0},{1,1,1}}};
const Shape L_shape = {2,3, (char [][3]){{0,0,1},{1,1,1}}};
const Shape ML_shape = {2,3, (char [][3]){{1,0,0},{1,1,1}}};
const Shape SQ_shape = {2,2, (char [][2]){{1,1},{1,1}}};
const Shape R_shape = {1,4, (char [][4]){{1,1,1,1}}};

int main() {
    return 0;
}

它不起作用。这是gcc错误代码:

    tetris.c:11:1: warning: initialization from incompatible pointer type [enabled by default]
 const Shape S_shape = {2,3, (char [][3]){{0,1,1},{1,1,0}}};
 ^
tetris.c:11:1: warning: (near initialization for ‘S_shape.shape’) [enabled by default]
tetris.c:12:1: warning: initialization from incompatible pointer type [enabled by default]
 const Shape Z_shape = {2,3, (char [][3]){{1,1,0},{0,1,1}}};
 ^
tetris.c:12:1: warning: (near initialization for ‘Z_shape.shape’) [enabled by default]
tetris.c:13:1: warning: initialization from incompatible pointer type [enabled by default]
 const Shape T_shape = {2,3, (char [][3]){{0,1,0},{1,1,1}}};
 ^
tetris.c:13:1: warning: (near initialization for ‘T_shape.shape’) [enabled by default]
tetris.c:14:1: warning: initialization from incompatible pointer type [enabled by default]
 const Shape L_shape = {2,3, (char [][3]){{0,0,1},{1,1,1}}};
 ^
tetris.c:14:1: warning: (near initialization for ‘L_shape.shape’) [enabled by default]
tetris.c:15:1: warning: initialization from incompatible pointer type [enabled by default]
 const Shape ML_shape = {2,3, (char [][3]){{1,0,0},{1,1,1}}};
 ^
tetris.c:15:1: warning: (near initialization for ‘ML_shape.shape’) [enabled by default]
tetris.c:16:1: warning: initialization from incompatible pointer type [enabled by default]
 const Shape SQ_shape = {2,2, (char [][2]){{1,1},{1,1}}};
 ^
tetris.c:16:1: warning: (near initialization for ‘SQ_shape.shape’) [enabled by default]
tetris.c:17:1: warning: initialization from incompatible pointer type [enabled by default]
 const Shape R_shape = {1,4, (char [][4]){{1,1,1,1}}};
 ^
tetris.c:17:1: warning: (near initialization for ‘R_shape.shape’) [enabled by default]

Gcc: (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4 谢谢!

已解决 https://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html

【问题讨论】:

  • 使用固定大小的数组会更好:char shape[2][4]
  • 我知道这行得通。但我很想知道我做错了什么。
  • 这样会更优雅。
  • 顺便说一句,我无法做到这一点,因为在轮换时我将向后循环阵列。
  • 好吧:char shape[4][4]

标签: c arrays gcc multidimensional-array


【解决方案1】:

您遗漏了一些 compound literals 并在您正在使用的类型中使用了错误的类型。以下是您可能想要的一个小示例:

const Shape S_shape = {
        2, /* height */
        2, /* width */
        (char *[]) { /* Compound literals, declaring an anonymous array of `char *` with static storage duration */
                (char []) {0, 1}, /* Another compound literal, declaring a static storage duration for a `char []` that will be pointed by `char *[]` */
                (char []) {1, 1} /* Same as above, this one the next (and last) element of `char *[]` */
        }
};

没有 cmets(为了便于阅读):

const Shape S_shape = {
        2,
        2,
        (char *[]) {
                (char []) {0, 1},
                (char []) {1, 1}
        }
};

https://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html

【讨论】:

猜你喜欢
  • 2016-12-09
  • 2016-08-16
  • 2020-03-30
  • 2015-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多