如何多维数组工作中的C指针 [英] How Pointer of Multi-Dimension Arrays Work in C

查看:124
本文介绍了如何多维数组工作中的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指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆