-1

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.

ST3
  • 8,826
  • 3
  • 68
  • 92
  • Please specify the programming language in your tags, and provide a code sample describing what you mean. – Robert Harvey Jul 30 '13 at 17:37
  • If your matrix elements are stored contiguously in memory (which is fairly common) you just pass a pointer to the first element (NOT a pointer to a pointer). It depends entirely on how the matrix is arranged in memory. It could be a sparse matrix represented by a linked list, or something else entirely even. – Greg Prisament Jul 30 '13 at 17:43
  • Do you know "why pointer is a array?" (in some expressions) – Grijesh Chauhan Jul 30 '13 at 17:44

2 Answers2

7

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

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],

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 |   |   |   |   |   |   |
                +---+---+---+---+---+---+---+
                |   |   |   |   |   |   |   |
                +---+---+---+---+---+---+---+
                |   |   |   |   |   |   |   |
                +---+---+---+---+---+---+---+
                |   |   |   |   |   |   |   |
                +---+---+---+---+---+---+---+
                |   |   |   |   |   |   |   |
                +---+---+---+---+---+---+---+
                |   |   |   |   |   |   |   |
                +---+---+---+---+---+---+---+

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

  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 One more thing, pointer to pointer not always a matrix.

ST3
  • 8,826
  • 3
  • 68
  • 92
  • if you need diagrams [read](http://stackoverflow.com/questions/17564608/what-does-the-array-name-mean-in-case-of-array-of-char-pointers/17661444#17661444) I also explained a 2-D matrix there. – Grijesh Chauhan Jul 30 '13 at 18:28
  • I think it would help if you provided an example where a pointer to pointer points eventually into a matrix. – Johannes Schaub - litb Jul 30 '13 at 18:32
0

Why pointer to pointer is a matrix?

This is not true for every case.

A pointer can also be a pointer to a single element and and therefore a "pointer to pointer" can also be a pointer to a single pointer which points to a single element.

A pointer to pointer only is a valid (dense) "matrix", if

  1. it's an array of pointers,
  2. each of which points to an array of elements,
  3. with each element array having the same size.

(If this is about C++: Don't use pointer-to-pointer syntax at all but stick to a 1d representation something like this instead.)

Community
  • 1
  • 1
Pixelchemist
  • 24,090
  • 7
  • 47
  • 71