2016-07-13 5 views
0

私は問題を抱えています。 私はテトリスを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 ありがとう!

は、あなたは、いくつかのcompound literalsが不足して、あなたが使用しているもので間違った型を使用している https://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html

+2

あなたが固定サイズの配列を使用してオフにはるかに良いでしょう: 'char shape [2] [4]' – user3386109

+0

私はそれが動作することを理解しています。しかし、私は何が間違っていたのかを知ることに興味があります。 –

+0

また、この方法でよりエレガントになります。 –

答えて

3

を解決しました。ここであなたが望むかもしれないものの小さな例です。(読みやすさ)コメントなし

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 *[]` */ 
     } 
}; 

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

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

+1

ああ!ありがとう、私はこれについて知らなかった。 https://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html –

+0

@NajibGhadri yw :) – pah