如何多维数组工作中的C指针

编程入门 行业动态 更新时间:2024-10-28 14:27:29
本文介绍了如何多维数组工作中的C指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我与指针的概念进行实验,以C.多维数组假设我想处理通过功能的多维数组。在code还挺看起来是这样的:

I'm experimenting with the concept of pointer to multi-dimension array in C. Suppose I want to process a multi-dimensional array via a function. The code kinda looks like this:

#include <stdio.h> void proc_arr(int ***array) { // some code } int main(int argc, char **argv) { int array[10][10]; for(int i = 0; i < 10; i++) { for(int j = 0; j < 10; j++) { array[i][j] = i * j; } } proc_arr(&array); return 0; }

问题是,当我要访问阵列在 proc_arr ,我不能。从我的理解,我们应该访问它是这样的:

The problem is, when I want to access array inside proc_arr, i can't. From my understanding, we should access it this way:

void proc_arr(int ***array) { (*array)[0][1] = 10; }

所以我derefer的阵列来告诉我要到该地址并获得价值的编译器。但不知何故,它崩溃。我试过的 * 和括号几个组合仍然不能使它发挥作用。我是pretty确保它是因为我不理解指针的指针和指针。

So I derefer the array to tell the compiler that I want to go to that address and get the value. But somehow, it crashes. I've tried several combinations of * and parentheses and still can't make it work. I'm pretty sure it's because of me not understanding pointers and pointers of pointers.

哦,我注意到,这是不同的,如果我们有一个的char **工作(字符串数组)太像了的argv和envp。但对于envp,不知何故,我可以用访问(* envp)。为什么呢?

Oh, and I've noticed that it's different if we work with a char **(array of string) too, like for argv and envp. But for envp, I somehow can access it with (*envp). Why?

下面是procces envp的函数(工作):

Here's the function that procces envp (and worked):

int envplen(char ***envp) { int count = 0; while((*envp)[count] != NULL) { count++; } return count; }

另外,我可以以某种方式访问​​ envp 在 envplen 函数仅 envp ,但还是按引用传递呢?

Also, can I somehow access envp in the envplen function with only envp, but still pass it by reference?

谢谢了。

推荐答案

现在的问题是,因为 int数组[10] [10] 在栈中分配并不铺陈上内存你觉得它的方式。这是因为的数组不是指针的。内存仍然是一个线性阵列布局,而不是一个二维阵列,即使这就是下标可能表示。换句话说,对于 int数组的存储[10] [10] 如下所示:

The problem is because int array[10][10] allocated on the stack does not lay out memory the way you think it does. This is because arrays are not pointers. The memory is still laid out in a linear array, not a "two dimensional" array, even though that's what the subscripts might indicate. In other words, the memory for int array[10][10] looks like the following:

starting address: ending address: | Block_1 of 10 int | Block_2 of 10 int | ... | Block_10 of 10 int |

所以,当你的隐式数组转换为 INT *** ,然后尝试访问像(*数组)数组[1] [10],是什么这实际上转化为是像 *(*((*数组)+ 1)+ 10),并为这种操作内存布局希望看到内存设置,如以下内容:

So when you implicitly convert the array to an int***, and then try to access the array like (*array)[1][10], what this actually translates to is something like *(*((*array) + 1) + 10), and the memory layout for such an operation wants to see memory setup like the following:

int*** array | | | Pointer | | | | Pointer_0 | Pointer_1 | ... | Pointer 10 | | | | | | | Block of 10 int | | | | | Block of 10 int | | |Block of 10 int|

更多推荐

如何多维数组工作中的C指针

本文发布于:2023-07-29 08:34:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239117.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多维   数组   指针   工作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!