I am a beginner with C++ so please excuse my ignorance.
I am attempting to pass a dynamically created 2d array to a function.
The array the [3]x[variable] array is created like this.

//Create array for the transformed coords
	double **transf;
	transf = new double*[3];
	for (int i=0;i<atoms;i++){
		cout << i;
		transf[i]=new double[atoms];
	}

I declare the function and call it

void printdata(int, string*, double* , double*,double**);
int main (int argc, char **argv)
{
...
printdata(allatoms ,name, rot1, rot2, transf);
...}

Here is the function.

void printdata(int allatoms ,string* name[], double rot1[], double rot2[], double** transf[][]){
	for (int i=0; i<allatoms; i++){
		if (i==0){
			cout << name[i];
			for (int r1=0; r1<3; r1++){
				printf("  %*.*f",15,8,rot1[r1] );
			}
			cout << endl;
		}
		if (i==1){
			cout << name[i];
			for (int r2=0; r2<3; r2++){
				printf("  %*.*f",15,8,rot2[r2] );
			}
			cout << endl;
		}
		if (i>1){
			cout << name[i];
			for (int r3=0; r3<3; r3++){
				printf("  %*.*f",15,8,transf[r3][i-2] );
			}
			cout << endl;
		}
	}
}

When I try to compile I get

rotation.cpp(225): error: parameter type involves pointer to array of unknown bound
void printdata(int allatoms ,string* name[], double rot1[], double rot2[], double** transf[][]){

rotation.cpp(244): error: expression must be a pointer to a complete object type
printf(" %*.*f",15,8,transf[r3][i-2] );

How can I pass this array?
I would just declare the variables as global variables but the size of the array is passed in an argument to the program.

Recommended Answers

All 2 Replies

When an array has more than 1 dimension (call it n-dimensions), you must specify the sizes of dimensions 2 thru n when passing it to a function. You may only leave the first dimension undefined.

I'm not sure how you would do that in your situation.

An array is nothing more than a pointer to the first element with a series of contiguous offsets from the starting address. Have you tried just passing the pointer instead of the whole array?

don't put the [] on the parameter in the function header, just the **

//not
double** transf[][] )
//just
double** transf )

What you're describing is a two-D array of 2-D arrays!

commented: It doesn't mean that; an array is not a pointer. A pointer to pointer is not a pointer to a 2D array. -2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.