2017-01-31 4 views
0

を使用して' enum Zahlen 'タイプを初期化するときに、エラーが発生する可能性のある型は互換性がありません。私はすべてが正常に動作構造体s_actionに列挙Zahlenを省略するとタイプ 'struct s_action(*)

[Error] incompatible types when initializing type 'enum Zahlen' using type 'struct s_action (*)[20]' 

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


typedef enum Zahlen { 
    eins =0, 
    zwei, 
    drei 

}tZahlen; 

struct s_action{ 
    tZahlen aZahl; 
    void *argument; 
    char name[]; 
}; 

struct s_testschritt{ 
    int actioncount; 
    struct s_action actions[]; 
}; 

struct s_action myactions[20]; 

struct s_testschritt aTestschritt = { 
    .actioncount = 20, 
    .actions = &myactions 

}; 

int main(int argc, char *argv[]) { 

    return 0; 
} 

これは私のコンパイル時に次のエラーを与える: は、ここでは、コードです。しかし、私は私の構造体s_actionでこの列挙体が必要です。

これを正しく定義して初期化するにはどうすればよいですか?

+0

問題は 'actions'フィールドにあります。 **配列メンバーを割り当てることはできません**。ポインタや 'memcpy'を使います。 –

+0

dbushの答えは、警告を取り除くべきです。しかし、 's_action'構造体にも柔軟な配列メンバが含まれており、ストレージが割り当てられていないという問題が残っています。構造体が可撓性アレイ部材を有する場合、例えば、 'char name []'、それらの構造体の配列を宣言することはできません。 'struct s_action myactions [20]'です。その配列宣言は 'name'メンバのためのスペースを予約しません。 – user3386109

+0

@JakubKaszycki:それは割り当てではなく、イニシャライザーです。 OPが正しいタイプを使用していれば動作します。 – Olaf

答えて

2

フィールドactionsstruct s_testschrittは、フレキシブルなアレイメンバーです。配列(または配列へのポインタ)を代入することはできません。

このメンバーをポインタとして宣言します。次に、配列myactionsで初期化し、最初の要素へのポインタに減衰します。

struct s_testschritt{ 
    int actioncount; 
    struct s_action *actions; 
}; 

struct s_action myactions[20]; 

struct s_testschritt aTestschritt = { 
    .actioncount = 20, 
    .actions = myactions 

};