problem with properly passing a multidimensional array to a function

This is a sample of what I'm trying to do

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
44
45
46
"main.h"
extern const int N;
extern const int R;

"main.cpp"
#include <main.h>
const int N = 100;
const int R = 2;
double G1[3][N][R];
double G2[3][R]={ 0 }
double U[3][N][R] = { 0 };

double fun1(double G1[][N][R],double G2[][R], int, int);

int main(){

G1[0][0][0] = 1;
G1[1][0][0] = 2;
G1[2][0][0] = 3;

for (int i = 0; i < R ; i++){
     for (int j=0; j < N ; j++){

           fun1(G1,G2,i,j);
           G1[0][j+1][i] = G1[0][j][i] + (3*U[0][j][i]);
           G1[1][j+1][i] = G1[1][j][i] * 3*U[1][j][i];
           G1[2][j+1][i] = G1[2][j][i] - (3*U[2][j][i]);

 
     }
}
return 0;
}
"fun1.cpp"
#include <main.h>

double fun1(double G1[][][],double G2[][], int i , int j){

U[0][j][i] = G1[0][j][i] + G2[0][i];
U[1][j][i] = G1[1][j][i] + G2[1][i];
U[2][j][i] = G1[2][j][i] + G2[2][i];

return U[0][j][i];
return U[1][j][i];
return U[2][j][i];
}


Clearly the way I defined fun1 is incorrect since N and R are initialized in the main function, and they are unknown to fun1. What would be the right approach to define fun1 knowing that all G1, G2 and U are fixed-dimension arrays. Any help would be appreicated.

P.S. When I put everything in one .cpp I have no problems. However, I'm trying to split my legnthy program into several .cpp files.

Thanks
1
2
3
return U[0][j][i];
return U[1][j][i];
return U[2][j][i];

What.

Move the definition of fun1() to main.h.
Last edited on
I guess the way I returned the U is incorrect, I should've just returned as an array. Moving the definition of fun1() to the header file didn't solve my problem. I still get lots of errors such as missing subscript. The way the fun1() is incorrect and I want to know how I can correct it. The dimensions N and R are NOT known except at the main function.

Thanks helios
What is the exact error message? You should have posted that to begin with.
These are the errors I get when running:

1
2
3
4
>.\fun1.cpp(3) : error C2087: 'G1' : missing subscript
>.\fun1.cpp(3) : error C2087: 'G1' : missing subscript
>.\fun1.cpp(3) : error C2087: 'G2' : missing subscript
>.\fun1.cpp(5) : error C2065: 'U' : undeclared identifier


I understand error C2065 as U obviously is not identified

I want to know the proper way to define fun1, please.

Thanks again
This calls for verifying whether the most obvious course of action is valid: copy the declaration to the definition (keep the parameter names!).
Sorry but I don't get what are you saying exactly. I have the same parameters names. I don't want to initialize N and R except at the main function. Still looking for a further assistance. Thanks
I mean what I said. There's no way I can make it any more explicit: copy the declaration to the definition. You didn't put the parameter names in the declaration, which is why I told you to be careful not to erase them.

Declaration:
double fun1(double G1[][N][R],double G2[][R], int, int);
Definition:
double fun1(double G1[][][],double G2[][], int i , int j){/*...*/}
Last edited on
There are a couple of things about your code in the initial post.

Lines 44 and 45 will never be reached because the return statement on line 43 effectively terminates the function fun1. I would suggest passing a pointer to U as a parameter to fun1() instead.

With the errors, which lines in your original post do the errors get reported against?
Lines 37 and 11. Durr.
@helios
well it's not 11 because that's the wrong file. Line 37 when being compiled has no 'visibility' of line 11 so the mismatch is irrelevant.


EDIT: OK got it, it needs the array bounds for the arrays to be copied into the function, so the first bracket can be left blank, but G1 second and third dimensions and G2 second dimension needs the size to be specified.
Last edited on
My bad. I got confused. It's line 43.
Topic archived. No new replies allowed.