How to pass 1 column of a 2D matrix to a function in C/C++

arrays, c, c++, multidimensional-array

Solution

You have 3 options,

1) Pass a pointer to your object (after moving it to the first element of the destination column)

twoDArray[0][column]

Now you can calculate the next item for this column (by jumping through the elements)

2) Create a wrapper class that would do this for you.

custom2DArray->getCol(1);
.
.
.
class YourWrapper{
 private:
   auto array = new int[10][10];
 public:
   vector<int> getCol(int col);
}

YourWrapper:: vector<int> getCol(int col){
  //iterate your 2d array(like in option 1) and insert values 
  //in the vector and return
}

3) Use a 1d array instead. You can get this info easily. By jumping through rows and accessing the value for the desired column.(Mentioning just for the sake of mentioning, don't hold it against me)

Problem

I have a 2D C-style array from which I have to pass just one column of it to a function. How do I do that? Basically I need the C/C++ equivalent of the MATLAB command `A[:,j]` which would give me a column vector. Is it possible in C/C++?

Original source