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



我读过一些关于向多维数组传递指针的材料,但我无法让它为自己工作。

我有:

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

不是一个真正的答案,而是一个需要一些格式的注释。:-)很容易理解C中的"数组"只是指针算术。例如:

char* ptr = "abcd";
    printf("Letter = %cn", ptr[1]);
    printf("Letter = %cn", 1[ptr]); // Same damn thing!
    printf("Letter = %cn", *(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*)添加到矩阵和间接。然后将3*sizeof(int)添加到该矩阵和间接中。"

@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;

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

相关内容

  • 没有找到相关文章

最新更新