1

私は今少し絶望しているので、あなたが私を助けてくれることを願っています. 私はスレッドで作業していますが、それらすべてに同じ 2D 配列を変更してもらいたいです。そこで、2D 配列へのポインターを持つ構造体を作成しましたが、機能しません。これは私のコードです

typedef struct {
    char XY;
    int b;
    int d;
    int f;
    int h;

} celdaMatriz;

typedef struct {
    int totalX;
    int totalY;
    celdaMatriz **matriz;
    char direction;
    int i;
    int j;
    int cont;

} parameter;

ご覧のとおり、パラメーターには変数 "celdaMatriz matriz**" があります (私のコードのスペイン語については申し訳ありません)。"celdaMatriz" 2D 配列を初期化する関数が追加されました

void createMatriz(int totalX, int totalY, celdaMatriz **matriz[totalX][totalY])
{
    int i,j;
    for(i=0;i<totalX; i++){
        for(j=0; j<totalX;j++){
            matriz[i][j]->XY=' ';
            matriz[i][j]->b=0;
            matriz[i][j]->d=0;
            matriz[i][j]->f=0;
            matriz[i][j]->h=0;
        }
    }

    matriz[0][3]->XY='/';
    matriz[2][0]->XY='*';
    matriz[2][1]->XY='*';
    matriz[0][2]->XY='*';
    matriz[2][2]->XY='*';
    matriz[1][3]->XY='*';
    matriz[3][3]->XY='*';
}

関数 createMatriz は変更されますが、それがアイデアです。だから今私の主な機能

int main( int argc,char *argv[])
{
    int i,j;
    celdaMatriz **matriz = malloc( sizeof (*matriz) * 4);
    for (i=0; i < 4; ++i)
        matriz[i] = malloc(sizeof (**matriz) * 4);

     crateMatriz(4,4,matriz); 
    parameter *p1,p2;

    parameter *p1 = malloc(sizeof(*p1));
    p1->totalX=4;
    p1->totalY=4;
    p1->i=0;
    p1->j=0;
    p1->cont=0;
    p1->direction= 'f';
    p1->matriz= matriz;
    return 0;
}

そのため、新しいスレッドを作成するときはいつでも、新しいパラメーター変数を作成します。その "matriz" 変数は、メイン関数で作成した 2D 配列へのポインターです。少なくともそれがアイデアですが、これらのエラーが発生します

 **request for member 'XY' in something not a structure or union**
 **request for member 'b' in something not a structure or union**
 **request for member 'd' in something not a structure or union**
 **request for member 'f' in something not a structure or union**
 **request for member 'h' in something not a structure or union**
  passing argument 3 of 'crearMatriz' from incompatible pointer type [enabled by        default]
    main.c: 59:6: note: expected 'struct celdaMatriz ** (*) [(unsigned int) (totaly)]'         but argument is of type 'struct celdaMatriz **

エラーは createMatrix 関数から発生し、最後のエラーは main 関数から発生します。ところで、私は Linux と C を使用しています。ありがとうございます。あなたが私を助けてくれることを願っています。

編集済み

エラーを修正しました。これ *互換性のないポインター型から 'clearMatriz' の引数 3 を渡しています [デフォルトで有効] main.c: 59:6: note: expected 'struct celdaMatriz * ( ) [(unsigned int) (totaly)] ' ただし、引数は 'struct celdaMatriz * の型です * 既に修正されています。したがって、問題は、私が matriz[i][j]->XY=' '; にアクセスしていることです。私は今少し絶望しているので、あなたが私を助けてくれることを願っています. 私はスレッドで作業していますが、それらすべてに同じ 2D 配列を変更してもらいたいです。そこで、2D 配列へのポインターを持つ構造体を作成しましたが、機能しません。これは私のコードです

typedef struct {
    char XY;
    int b;
    int d;
    int f;
    int h;

} celdaMatriz;

typedef struct {
    int totalX;
    int totalY;
    celdaMatriz **matriz;
    char direction;
    int i;
    int j;
    int cont;

} parameter;

ご覧のとおり、パラメーターには変数 "celdaMatriz matriz**" があります (私のコードのスペイン語については申し訳ありません)。"celdaMatriz" 2D 配列を初期化する関数が追加されました

void createMatriz(int totalX, int totalY, celdaMatriz **matriz[totalX][totalY])
{
    int i,j;
    for(i=0;i<totalX; i++){
        for(j=0; j<totalX;j++){
            matriz[i][j]->XY=' ';
            matriz[i][j]->b=0;
            matriz[i][j]->d=0;
            matriz[i][j]->f=0;
            matriz[i][j]->h=0;
        }
    }

    matriz[0][3]->XY='/';
    matriz[2][0]->XY='*';
    matriz[2][1]->XY='*';
    matriz[0][2]->XY='*';
    matriz[2][2]->XY='*';
    matriz[1][3]->XY='*';
    matriz[3][3]->XY='*';
}

関数 createMatriz は変更されますが、それがアイデアです。だから今私の主な機能

int main( int argc,char *argv[])
{
    int i,j;
    celdaMatriz **matriz = malloc( sizeof (*matriz) * 4);
    for (i=0; i < 4; ++i)
        matriz[i] = malloc(sizeof (**matriz) * 4);

     crateMatriz(4,4,matriz); 
    parameter *p1,p2;

    parameter *p1 = malloc(sizeof(*p1));
    p1->totalX=4;
    p1->totalY=4;
    p1->i=0;
    p1->j=0;
    p1->cont=0;
    p1->direction= 'f';
    p1->matriz= matriz;
    return 0;
}

そのため、新しいスレッドを作成するときはいつでも、新しいパラメーター変数を作成します。その "matriz" 変数は、メイン関数で作成した 2D 配列へのポインターです。少なくともそれがアイデアですが、これらのエラーが発生します

 **request for member 'XY' in something not a structure or union**
 **request for member 'b' in something not a structure or union**
 **request for member 'd' in something not a structure or union**
 **request for member 'f' in something not a structure or union**
 **request for member 'h' in something not a structure or union**
  passing argument 3 of 'crearMatriz' from incompatible pointer type [enabled by        default]
    main.c: 59:6: note: expected 'struct celdaMatriz ** (*) [(unsigned int) (totaly)]'         but argument is of type 'struct celdaMatriz **

エラーは createMatrix 関数から発生し、最後のエラーは main 関数から発生します。ところで、私は Linux と C を使用しています。ありがとうございます。あなたが私を助けてくれることを願っています。

編集

エラーを修正しました。これ *互換性のないポインター型から 'clearMatriz' の引数 3 を渡しています [デフォルトで有効] main.c: 59:6: note: expected 'struct celdaMatriz * ( ) [(unsigned int) (totaly)] ' ただし、引数は 'struct celdaMatriz * の型です * 既に修正されています。したがって、問題は、私が matriz[i][j]->XY=' '; にアクセスしていることです。間違って

編集こんにちは。私の問題に対する答えをすでに見つけたことをお知らせしたいと思います。マトリックスへのポインターを送信する代わりに、それをグローバル変数として使用します。これにより、すべてのスレッドがそれを変更できるようになります。したがって、解決策は 1-グローバル celdaMatriz **matriz を宣言します

celdaMatriz **matriz;

次に、メモリ割り当てを変更しました

matriz = (celdaMatriz **) malloc(4 * sizeof(celdaMatriz *));

for (i = 0; i < 4; ++i)
{
    matriz[i] = (celdaMatriz *) malloc(4 * sizeof(celdaMatriz));
}

最後に、新しい void createMatriz 関数:

void crearMatriz(int totalX, int totalY)
{
    int i=0,j=0;
    for(i=0; i<totalX;i++){
        for(j=0; j<totalY; j++){
            celdaMatriz *nodo = malloc(sizeof(celdaMatriz));
            nodo->b=0;
            nodo->d=0;
            nodo->f=0;
            nodo->h=0; 
            nodo->casilla=0;
            matriz[i+j*totalY]=(celdaMatriz *)nodo;

        }
    }

    matriz[12]->casilla=1; 
    for(i=0; i<totalX; i++){
        for(j=0; j<totalY; j++){
            printf(" %d ", matriz[i+j*totalY]->casilla); 
        }
        printf("\n"); 
     }
}

以上です。この問題で私を助けてくれた皆さんに感謝します

4

1 に答える 1

1

creatMatriz問題は、このように電話をかけようとしたことだと思いますが、

      createMatriz(p1->totalX, p1->totalY, p1->matriz);

C (C99) はそれを好まない。問題はそれp1->matrizがタイプであることですが、Cが無視するためstruct celdaMatriz **、あなたのプロトタイプはaをcreateMatriz要求しました。struct * (*) [totalY][totalX]

の定義を変更せずに保持したい場合はparameter、ダブルポインターを使用することをお勧めします。

于 2013-09-22T23:59:06.133 に答える