我已经阅读了一些有关将指针传递给多维数组的 Material ,但我无法让它为我自己工作。
我有:
/* 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) =警告:来自不兼容指针类型的赋值。
Error @ (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 = %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*) 添加到矩阵并间接。然后将 3*sizeof(int) 添加到该矩阵并间接。”
关于c - 如何将指针传递给抽象数据类型的多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14948879/