Search code examples
c++carraysmultidimensional-arrayvariable-assignment

How to get column of a multidimensional array in C/C++?


int matrix[9][9],*p;
p=matrix[0]; 

this works and gives first row of matrix, but how to get first column of matrix I've tried p=matrix[][0]; ? Also I don't understand why below code gets compiler error ?

int matrix[9][9],p[9];  // it looks really ugly, byt why it doesn't work ?
p=matrix[0];            // compiler gives "invalid array assigment"

is it because multidimensional arrays are arrays of arrays - and we should interpret matrix[i][j] as j-th element of i-th nested array ?


Solution

  • In C/C++, multidimensional arrays are actually stored as one dimensional arrays (in the memory). Your 2D matrix is stored as a one dimensional array with row-first ordering. That is why getting a column out of it is not easy, and not provided by default. There is no contiguous array in the memory that you can get a pointer to which represents a column of a multidimensional array. See below:

    When you do p=matrix[0], you are just getting the pointer to the first element matrix[0][0], and that makes you think that you got the pointer to first row. Actually, it is a pointer to the entire contiguous array that holds matrix, as follows:

    matrix[0][0]
    matrix[0][1]
    matrix[0][2]
    .
    .
    matrix[1][0]
    matrix[1][1]
    matrix[1][2]
    .
    .
    matrix[8][0]
    matrix[8][1]
    matrix[8][2]
    .
    .
    matrix[8][8]
    

    As seen above, the elements of any given column are separated by other elements in the corresponding rows.

    So, as a side note, with pointer p, you can walk through the entire 81 elements of your matrix if you wanted to.