2016-07-13 9 views
0

Ich habe ein Noob-Problem. Ich mache Tetris in c. Ich möchte einen Doppelzeiger in einer Struktur inline für jede Instanz initialisieren. Die Breite des Arrays ist unterschiedlich, aber in einer anderen Variablen definiert. Code:Initialisierung von multidimensionalen Zeigern mit verschiedenen dimensionalen anonymous Arrays inline

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

Es funktioniert nicht. Hier ist der gcc Fehlercode:

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 Dank!

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

+2

Sie werden viel besser eine feste Größe Array: 'Char Form [2] [4]' – user3386109

+0

Ich verstehe, dass das funktioniert. Aber ich bin interessiert zu wissen, was ich falsch gemacht habe. –

+0

Auch wäre es so eleganter. –

Antwort

3

Sie einige compound literals und mit dem falschen Typ in dem einen fehlt Sie verwenden. Hier ist ein kleines Beispiel von dem, was Sie vielleicht:

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

Ohne die Kommentare (zur besseren Lesbarkeit):

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

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

+1

Aha! Vielen Dank, ich wusste nichts davon. https://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html –

+0

@NajibGhadri yw :) – pah

Verwandte Themen