J
JR
Hey all,
I am passing a two dimensional array to a function. It basically
looks like this
int test(double array2 [3] [2], const CONST1, const CONST2)
{
int returnValue;
for (int i = 0; i<=CONST1; ++i)
{
for (int j = 0; j<=CONST2; ++j)
{
returnValue = 1; /*just a sample since I am return this value to
main */
}
}
return(returnValue);
}
int main()
{
const CONSTA = 3;
const CONSTB = 2;
double array1 [CONSTA] [CONSTB] = {{1,2},{3,4},{5,6}};
test(array1, CONSTA, CONSTB);
return(0);
}
Note: There is a little extra code so my second question might make
more sense.
Now what I really want (I think) is to pass the CONSTA/CONSTB values
instead of hard coding the numbers (2 and 3) in the test function. In
theory it might look like this:
int test(double array2 [CONSTA] [CONSTB], const CONST1, const
CONST2)
However, when I try it I get "undeclared identifier". I assume this
is because the test function does not know about CONSTA and CONSTB.
Is there a way to use the constants in the main function or just
somehow tell it to send the whole array? Please note I am VERY new to
C++ so please try to talk down to my level.
Now let me ask a related question about best practices.
The idea behind this is I can change the number of elements in either
of the two dimensions and only have to change my constants in the main
function. This means I do not have to go into the test function and
modify it. Of course I could make the two constants and the array
global. Am I going about this the right way or should I take a
different approach.
thanks in advance.
James
I am passing a two dimensional array to a function. It basically
looks like this
int test(double array2 [3] [2], const CONST1, const CONST2)
{
int returnValue;
for (int i = 0; i<=CONST1; ++i)
{
for (int j = 0; j<=CONST2; ++j)
{
returnValue = 1; /*just a sample since I am return this value to
main */
}
}
return(returnValue);
}
int main()
{
const CONSTA = 3;
const CONSTB = 2;
double array1 [CONSTA] [CONSTB] = {{1,2},{3,4},{5,6}};
test(array1, CONSTA, CONSTB);
return(0);
}
Note: There is a little extra code so my second question might make
more sense.
Now what I really want (I think) is to pass the CONSTA/CONSTB values
instead of hard coding the numbers (2 and 3) in the test function. In
theory it might look like this:
int test(double array2 [CONSTA] [CONSTB], const CONST1, const
CONST2)
However, when I try it I get "undeclared identifier". I assume this
is because the test function does not know about CONSTA and CONSTB.
Is there a way to use the constants in the main function or just
somehow tell it to send the whole array? Please note I am VERY new to
C++ so please try to talk down to my level.
Now let me ask a related question about best practices.
The idea behind this is I can change the number of elements in either
of the two dimensions and only have to change my constants in the main
function. This means I do not have to go into the test function and
modify it. Of course I could make the two constants and the array
global. Am I going about this the right way or should I take a
different approach.
thanks in advance.
James