Passing a row/column of a 2D vector to a function

Jan 17, 2012 at 9:17pm
Hi,

I have define a class which has complex vector members:
class Node
{
public:
Node(void);
GVector r;
vector <complex<double> > Uhat;
vector <complex<double> > dUhatdx;
vector <complex<double> > dUhatdy;

double rho,u,v,p;
//there are 4x3 derivatives to be calculated:
//dudx,dudy,dudz ; dvdx,dvdy,dvdz ; dwdx,dwdy,dwdz ; dTdx,dTdy,dTdz
double drhodx,drhody,drhodz;
double dudx,dudy,dudz;
double dvdx,dvdy,dvdz;
double dwdx,dwdy,dwdz;
double dpdx,dpdy,dpdz;

void Update();
void Nullify();
~Node(void);
};

In the main program I have:

Node node[Imax+1][Jmax+1]

I want to transfer only one row or column of this array to a function. How can I do this?

Thank you for your help
Jan 17, 2012 at 10:49pm
closed account (o1vk4iN6)
You can pass the pointer to the first element to the function:


1
2
3
4
5
6
7
8
9
10

void foo(Node arr[], int size)
{

}

Node node[Imax+1][Jmax+1];

foo( node[1], Jmax + 1 );
Jan 18, 2012 at 12:40am
Thank you xerzi!
Wouldn't this only specify row? how can I use this for columns?
Jan 18, 2012 at 1:18am
closed account (o1vk4iN6)
Well a c-style 2d array is still stored linearly, so there is no way other than to know the size of the "steps" from one to another. You would need to pass the pointer for the entire array and specify the size of the 2d array like so:

1
2
3
4
void foo(Node arr[][100], int size)
{

}


Or you could use templating in this case to allow any dimension:

1
2
3
4
5
6
7

template<unsigned int T>
void foo(Node arr[][T], int size)
{

}


In either case, inorder to get access to those values you're going to need to know the size of each "row".
Last edited on Jan 18, 2012 at 1:21am
Jan 18, 2012 at 1:36am
I think I will go with the first method.

Thank you so much xerzi!
Topic archived. No new replies allowed.