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

烂漫一生 提交于 2019-12-18 09:00:48

问题


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 ?


回答1:


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.




回答2:


You can get the first column using a loop like

for(int i = 0; i < 9; i++)
{
    printf("Element %d: %d", i, matrix[i][0]);
}

I think the assignment doesn't work properly because you're trying to assign something's that's not an address to a pointer.

(Sorry this is c code)




回答3:


There is no difference between specifying matrix[81] or matrix[9][9]

matrix[r][c] simply means the same as matrix[9*r+c]

There are other containers better suited fort multidimensional arrays like boost::multi_array

http://www.boost.org/doc/libs/1_53_0/libs/multi_array/doc/index.html

Think of the bare array just like allocating a contiguous piece of memory. You, the programmer then has to handle this piece of memory yourself. The bare name of the array, e.g. matrix is a pointer to the first element of this allocated piece of memory. Then *(matrix+1) is the same as matrix[0][1] or matrix[1].




回答4:


p is an array of int, matrix[0] is a pointer..




回答5:


matrix itself is the nearest thing you can get to a column of the array, inasmuch as (matrix + 1)[0][0] is the same as matrix[1][0].




回答6:


If you want your matrix to contiguous locations, declare it as a one dimensional array and perform the row and column calculations yourself:

int contiguous_matrix[81];

int& location(int row, int column)
{
  return contiguous_matrix[row * 9 + column];
}

You can also iterate over each column of a row:

typedef void (*Function_Pointer)(int&);

void Column_Iteration(Function_Pointer p_func, int row)
{
  row = row * MAXIMUM_COLUMNS;
  for (unsigned int column = 0; column < 9; ++column)
  {
    p_func(contiguous_matrix[row + column]);
  }
}



回答7:


For static declared arrays you can access them like continuous 1D array, p = matrix[0] will give you the 1st column of 1st row. Then the 1D array can be accessed like p[i], *(p+i), or p[current_raw * raw_size + current_column).

The things are getting tricky if a 2D array is represented with **p as it will be interpreted as an array of pointers to 1D arrays.



来源:https://stackoverflow.com/questions/15258084/how-to-get-column-of-a-multidimensional-array-in-c-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!