如何将指针传递给抽象数据类型的多维数组

时间:2013-02-19 02:37:03

标签: c pointers multidimensional-array

我已经阅读了一些关于将指针传递给多维数组的材料,但我无法让它为自己工作。

我有:

/* 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 */

2 个答案:

答案 0 :(得分:1)

您在@A的错误是尝试将cell***分配给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;

如果没有更具体的案例和确切的代码,您很难理解您不理解的部分。希望这有点帮助。

答案 1 :(得分: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)添加到该和间接。”