为什么指针,指针是一个矩阵? [英] Why pointer to pointer is a matrix?

查看:203
本文介绍了为什么指针,指针是一个矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在某些情况下,指针的指针被描述为一个矩阵。有人可以解释,为什么这个工程呀?其中 C 属性允许呢?

I know that in some cases, pointer to pointer is described as a matrix. Can somebody explain, why this works in that way? Which C property allows it?

请不要张贴这样的答案,如指针的指针并不总是矩阵。的我知道,但我问为什么它在某些情况下,一个矩阵。

Please do not post such answers as Pointer to pointer not always matrix. I know that, but I am asking why it is a matrix in some situations.

推荐答案

[] 在线实现。 A [B] 其实就是 *(A + B)

Because of opeator [] inline implementation. a[b] actually is *(a + b)

所以,第一个 [] 选择了行和第二选择列。
因此,它是改编[M] [N] 的StepOne = *(ARR + M),其中 M = M *ñ stepTwo =的StepOne [N]

So the first [] chooses the row and the second chooses the column. So it is arr[m][n] the same as stepOne = *(arr + M) where M = m * n and stepTwo = stepOne[n],

这是一样的 *(的StepOne + N)。因此,这条产业链,我们看到后什么改编[M] [N] 相同 *(ARR M * N + N)

which is the same as *(stepOne + n). So after this chain we see what arr[m][n] is the same as *(arr m*n + n)

要确认什么是真正的可以检​​查这个短节目

To confirm what is true you can check this short program

int main()
{
    char arr[10][15];
    std::cout << sizeof(arr) << std::endl;           //150
    std::cout << sizeof(*arr) << std::endl;          //15
    std::cout << sizeof(arr[0]) << std::endl;        //15
    std::cout << sizeof(*arr[0]) << std::endl;       //1
    std::cout << sizeof(**arr) << std::endl;         //1
    std::cout << sizeof(arr[0][0]) << std::endl;     //1

    std::cout << arr << std::endl;                    //some number
    std::cout << arr+1 << std::endl;                  //some number + 15
    std::cout << &arr << std::endl;                   //some number
    std::cout << &arr+1 << std::endl;                 //some number + 150

    return 0;
}



                +---+---+---+---+---+---+---+
This is matrix: | E |   |   |   |   |   |   |
                +---+---+---+---+---+---+---+
                |   |   |   |   |   |   |   |
                +---+---+---+---+---+---+---+
                |   |   |   |   |   |   |   |
                +---+---+---+---+---+---+---+
                |   |   |   |   |   |   |   |
                +---+---+---+---+---+---+---+
                |   |   |   |   |   |   |   |
                +---+---+---+---+---+---+---+
                |   |   |   |   |   |   |   |
                +---+---+---+---+---+---+---+

我标志着入境信函E.比方说这是 INT M [6] [7] 。所以,让我们去反向方式。

I marked entry letter E. Lets say this is int M[6][7]. So lets go reverse way.


  1. &放,M - 指针的矩阵。如果增加或一个你会得到一些其他的数据减少了,这是不好的......因为的sizeof(M)相同的sizeof(int)的* 7 * 6

  2. &放大器; M [​​0] 指针矩阵的第一行。如果你增加它,你会去到下一行,因为的sizeof(M [0])是等于的sizeof(int)的* 7

  3. 因此,为了获取指向你需要做的条目及(M [0] [0])的sizeof(M [0] [0])是等于的sizeof(INT)

  1. &M - pointer to matrix. If you increase or decrease it by one you will get on some other data, this is bad... because sizeof(M) is the same as sizeof(int) * 7 * 6
  2. &M[0] pointer to the first row of matrix. If you increase it you will go to the next row, because sizeof(M[0]) is equals to sizeof(int) * 7.
  3. So to get pointer to entry you need to do &(M[0][0]) and sizeof(M[0][0]) is equals to sizeof(int).

因此​​,为了使图出了这一切:

So to make a graph out of all this:

           +---+---+---+---+---+---+---+
M[0] ----> | E |   |   |   |   |   |   |
           +---+---+---+---+---+---+---+

                +---+
M[0][0]  ---->  | E |
                +---+

我希望图的帮助,因为我在解释这个东西可不是那么好...

I hope graph help because I'm not so good in explaining this stuff...

BTW 一件事,指针的指针并不总是一个矩阵。

BTW One more thing, pointer to pointer not always a matrix.

这篇关于为什么指针,指针是一个矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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