5

編集- 反対票を投じた人は説明できますか? 裏付けとなる証拠と事前調査の証拠を添えて、明確な質問があります。なぜあなたが私に反対票を投じているのか理解したいのですが...?


gcc でコンパイルすると、次のエラーが発生します。

error: incompatible types when assigning to type ‘struct cell’ from type ‘void *

問題の行は次のとおりです。

    struct cell* cells = NULL;
    cells = malloc(sizeof(struct cell) * length);
    for (i = 0; i < length; i++) {
            cells[i] = malloc(sizeof(struct cell) * width);

ここここで説明されているように、私は適切なプロトコルに従っていると信じています。私は何が欠けていますか?

4

2 に答える 2

6

多次元配列の場合、次のタイプの配列が必要ですstruct cell** cells

struct cell** cells = NULL;
cells = malloc(sizeof(struct cell*) * length);
for(int i = 0; i < length; i++) {
  cells[i] = malloc(sizeof(struct cell)*width);
}

これcellsは多次元配列で、最初のインデックス範囲は長さ、2 番目のインデックス範囲は幅です。

于 2013-02-20T16:03:46.173 に答える