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

您在 @A 的错误是由于尝试将 a 分配cell***给 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 回答
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 这样经常使用它们的语言。但是,只要你有简单的数组和指针算法,你就可以自己动手。所以,如果我想要一个一维数组,我只需要一个指向 malloc() 提供的内存的指针。但是如果我想要一个二维数组,那么我需要一个指针数组,每个指针都指向 malloc() 返回的一些内存。因为这:

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

Matrix[2][3] = 0;

表示“将 2*sizeof(int*) 添加到 Matrix 和间接。然后将 3*sizeof(int) 添加到该矩阵和间接。”

于 2013-02-19T03:30:13.710 回答