Passing a multidimensional array to a function

Hi All - I have an array of data, x,y,z coordinates for natoms as a function of time. The data is in mydata[ntime][natom][2] where the last index 0,1,2 is x,y,z respectively. I would like to pass the entire array to a function but the function won't allow me to pass an array with two variable dimensions. There MUST be a way around this. As a matter of fact I used to be a proficient C programmer but haven't been coding in C++ for about 5 years now and I just forget. Any help is greatly appreciated.
You could pass a pointer to the first element and a count of how many elements are in the array.

Something like this:

1
2
3
4
5
6

return_type my_function(int *arr, int n_elems);

int xyz[2][2][2];

my_function(&xyz[0][0][0], 2*2*2);


There are problably much better aprouchs for this, but I don't use arrays anyway.

You could emulate a 3d array using a 1d one. Or much simplier, use vectors.
Might it be might be better if the function was?

return_type my_function(int *arr, int x_count, int y_count, int z_count);

Andy

P.S. But in C++ I'd prob. use a class which implementes a GetAt(x, y, z), etc. method.
Last edited on
I might be misunderstanding your question so I'll provide a few ideas.

1) Simplify your function. Don't worry about passing the entire 3-D array at once, if you can pass each element of the array as seperate arguments and process them with in the function that way. This may not be a valid suggestion but you don't tell us what you function actually does.

2) For the sake of organization how about you don't use multi-demensional arrays and you instead stick to objects (struct's or class's) instead?

3) You could pass a pointer to the first element in each dimension of the array to your function as seperate arguments then assign them to a dynamically created local array within the function. This is ugly C-style programming and you have to be careful but it could work.

EDIT: How much code do you have? Can we see this function?
Last edited on
Thanks for the suggestions guys. The function is designed to generate the dihedral angle defined by 4 atoms. Here are the functions:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
void cross(double a[2], double b[2], double c[2])
{
c[0]=a[1]*b[2]-a[2]*b[1];
c[1]=a[2]*b[0]-a[0]*b[2];
c[2]=a[0]*b[1]-a[1]*b[0];
}

double dp(double a[2], double b[2])
{
return(a[0]*b[0]+a[1]*b[1]+a[2]*b[2]);
}

double Sdihedral(int a1, int a2, int a3, int a4, double mydat[][82][2], int mytime){
double v1[2], v2[2], v3[2];
double size;
double v2v1[2];
double cp23[2],cp12[2],dp1,dp2;
double retval;
int myi;
for (myi=0;myi<3;myi++){
v1[myi]=mydat[mytime][a2][myi]-mydat[mytime][a1][myi];
v2[myi]=mydat[mytime][a3][myi]-mydat[mytime][a2][myi];
v3[myi]=mydat[mytime][a4][myi]-mydat[mytime][a3][myi];
}
size=pow((v2[0]*v2[0]+v2[1]*v2[1]+v2[2]*v2[2]),0.5);
v2v1[0]=v1[0]*size;
v2v1[1]=v1[1]*size;
v2v1[2]=v1[2]*size;

cross(v2,v2,cp23);
cross(v1,v2,cp12);
dp1=dp(v2v1,cp23);
dp2=dp(cp12,cp23);

retval=57.2957795*atan2(dp1,dp2);
if (retval<0){
retval=retval+360;
}
if (retval>360){
retval=retval-360;
}
return (retval);
}


so for the array 'mydat' in the dihedral function I assume that 82 atoms are being read into the array. Sounds like there are much better ways to do this though. I'll have to look into classes, etc.

Thanks again.
void cross(double a[2], double b[2], double c[2]);

looks wrong, given you're accessing elements with an index of 2.

You need double a[3], etc.

a[3] has three elements: a[0], a[1], a[2]

a[2] only two: a[0], a[1]
Last edited on
Seeing your code, it does look like a class based approach would make a lot of sense. Starting with 3d vectors and matrices.

Or would you consider using a library? If so, I have been looking about in this kind of area for my own reasons, and some of the libraries I found (the first two seem to be the most popular):

IT++
http://sourceforge.net/apps/wordpress/itpp/

Armadillo
http://arma.sourceforge.net/

The Blitz++ Library / An array library for C++
http://www.oonumerics.org/

boost::ublas
http://www.boost.org/doc/libs/1_41_0/libs/numeric/ublas/doc/index.htm

(Also see this list: http://techlogbook.wordpress.com/c-numerical-libraries/)
Last edited on
Topic archived. No new replies allowed.