0

多次元配列へのポインターの受け渡しに関する資料を読みましたが、自分で機能させることができません。

私は持っている:

/* This code basically, in order, does this--or tries to:
    - Create 2D array of cell structs
    - Create info_pass detailing certain attributes of the 2D array
         + in particular, I am trying to include a pointer to the 2D array so that I
           can pass the info_pass struct between functions and update the contents of 
           the 2D array in each function.
    - The updating is done in struct info_pass* update(...){}
    - ... however, in my full program, there are several other functions it is passed 
      to, so being able to pass a pointer that allows manipulation of the 2D array is
      what I'm really after.
*/

struct info_pass {
    /* stuff */
    struct cell* master;
};
struct cell {
    /* values */
    /* lots of pointers to other cells */
};
struct info_pass* genesis() {                 /* creating an the multiD array */
    /* stuff */
    struct cell* (*cells)[width];
    cells = malloc(sizeof(struct cell) * width * length);

    struct info_pass* keycard = NULL;
    keycard = malloc(sizeof(struct info_pass));
    /* assign values to key card */
    keycard->master = cells;     /* problem here?! */  <==== (A)

    /* update cells */

    return keycard;                           /* therefore problem here too */
}
struct info_pass* update(struct info_pass* key) {
    struct info_pass* keyRef = NULL;
    keyRef = malloc(sizeof(struct info_pass));
    keyRef = key;                             /* and of course here */

    struct cell* home1 = NULL;
    home1 = malloc(sizeof(struct cell));

    /*here I want to update the multidimensional array*/ <===== (B)
    /*... and then send it back ...*/
    return keyRef;
 }

エラー @ (A) = 警告: 互換性のないポインター型からの代入。

エラー @ (B) = エラー: 添字付きの値は配列でもポインターでもありません。

良い方向に向かうことを願うばかりです。

編集

ThePosey の提案に従って、「エラー: 添字付きの値はポインターでも配列でもありません」に関連するコードをさらに示します。将来のコンテキストのために元の質問の状態を保持するために、上記のコード サンプルに入れるのではなく、以下に追加します。

struct info_pass* update(struct info_pass* key) {      

    /* passing data, including a pointer to a 2D array from info_pass     */
    /* struct then I want to access the 2D array and change it's contents */
    /* contents and then send it back in another info_pass struct         */

    struct info_pass* keyRef = NULL;      
    keyRef = malloc(sizeof(struct info_pass));
    keyRef = key;                     /* to pass the info back afterwards */

    int len = keyRef->length;
    int wid = keyRef->width;

    struct cell* home1 = NULL;
    home1 = malloc(sizeof(struct cell));
    home1 = key->masterRef[len][wid];       /* to access and change the data */

    int fate = 0;
    int a = 0;
    int b = 0;

    for (a = 0; a < len; a++) {
            for (b = 0; b <  wid; b++) {
                    if (keyRef->masterRef[a][b].go_up.state == 1) { 
     /* just trying different styles of calls */
                            fate++;
                    } if (home1[a][b].go_down.state == 1) {
                            fate++;
                    } if (home1[a][b]->go_left->state == 1) {
                            fate++;
                    } if (home1[a][b]->go_right->state == 1) {
                            fate++;
     /* there more calls to the array, and all generate the same error: */
     /* subscripted value is neither array nor pointer */
4

2 に答える 2

1

本当の答えではありませんが、書式設定が必要なメモです。:-) C の「配列」は単なるポインタ演算であることを理解しておくと便利です。例えば:

char* ptr = "abcd";

    printf("Letter = %c\n", ptr[1]);
    printf("Letter = %c\n", 1[ptr]); // Same damn thing!
    printf("Letter = %c\n", *(1 + ptr)); // and again!

したがって、「配列のインデックス付け」のように見えることを行っているとき、C は単純に物事を追加し、それらを介して間接的に処理しています。構文 "x[y]" は、「x を y に加算し、結果をポインターとして使用する」ことを意味します。(もちろん、注意点は、C が整数をポインターに追加する前に、指されているもののサイズで整数を乗算することです)

IOW、[]演算子は実際には「追加と間接」を意味します。

古き良きANSI Cには多次元配列がありますか? そうではなく、それらを頻繁に使用する FORTRAN のような言語がそうであるという意味ではありません。ただし、単純な配列とポインター演算があれば、自分で作成できます。したがって、1 次元配列が必要な場合は、malloc() が提供するメモリへのポインタがあれば十分です。しかし、2 次元配列が必要な場合は、ポインターの配列が必要です。各ポインターは、malloc() が返したメモリを指します。これのため:

int** Matrix = MallocMatrix(3, 5);

Matrix[2][3] = 0;

「Matrix に 2*sizeof(int*) を追加して間接的に。次に、それに 3*sizeof(int) を追加して間接的に」という意味です。

于 2013-02-19T03:30:13.710 に答える
1

cell***@A でのエラーは、 aを aに割り当てようとしたことによるものcell*です。多次元 (コードからは、2D の長さ x 幅が必要なように見えます) 配列を作成する場合は、次のようにします。

struct cell* cells[length];

for (int i = 0; i < length; i++)
{
    //give each row width number of cell structs
    cells[i] = malloc(sizeof(struct cell) * width);
}

あなたの質問の残りの部分を手伝おうとしています。あなたは変わるだろう

struct info_pass {
    /* stuff */
    struct cell* master;
};

struct info_pass {
    /* stuff */
    struct cell** master;
};

ただし、配列のサイズがわかるように、その構造体に長さと幅の情報も保持する必要があるでしょう。その後、情報パスがある場所ならどこでも、次のようにして個々のセル要素にアクセスできます。

struct cell* single_cell = &my_info_pass->master[lengthIndex][widthIndex];

または、セル構造体に cell_id int のようなものがある場合は、値を直接取得します。たとえば、次のようになります。

int cell_value = my_info_pass->master[lengthIndex][widthIndex].cell_id;

より具体的なケースと正確なコードがなければ、理解していない部分を絞り込むのは困難です。うまくいけば、これは少し役立ちます。

于 2013-02-19T03:10:00.477 に答える