Passing an array to a function using a constant/variable

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
 
R

Rob Williscroft

JR wrote in in
comp.lang.c++:
int test(double array2 [3] [2], const CONST1, const CONST2)

There is no *implicit* int in C++ the above paramiter declaration's
(CONST1 and CONST2) are ill-formed.

Also note in C++ array's /decay/ to pointers, here (with implicit int :)
is how the compiler interprets you declaration above:

int test( double (*array2)[2], int const CONST1, int CONST2 )

I.e. only the second dimension is really used, IOW the major dimension
is lost.
{
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);

In C++ return is a statment:

return returnValue;

You should also consider all uppercase identifiers (CONST1) for
example as reserved for the preprocessor (#defines).

Anyway you appear to want a template function so here it is:

template < unsigned Const1, unsigned Const2 >
int test( double ( & array )[ Const1 ][ Const2 ] )
{
/* -- array passed by reference to prevent pointer decay -- */

int returnValue;
for ( int i = 0; i <= Const1 ; ++i )
{
for ( int j = 0; j <= Const2; ++j )
{
returnValue = 1;
}
}
return returnValue;
}

int main()
{
unsigned const A = 3;
unsigned const B = 2;

double array1[ A ][ B ] = {{1,2},{3,4},{5,6}};

test( array1 );

return 0;
}


Rob.
 
J

John Harrison

JR said:
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)

ITYM

int test(double array2 [3] [2], const int CONST1, const int CONST2)

It's not legal to miss out the int.
{
int returnValue;
for (int i = 0; i<=CONST1; ++i)
{
for (int j = 0; j<=CONST2; ++j)
{

These loops are wrong

for (int i = 0; i<CONST1; ++i)
{
for (int j = 0; j<CONST2; ++j)

You were going past the end of the array.
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.

Declare the constants as global, then you don't need to pass them to the
function at all.

const int CONSTA = 2;
const int CONSTB = 3;

int main()
{
double array1 [CONSTA] [CONSTB] = {{1,2},{3,4},{5,6}};
test(array1);
return 0;
}

int test(double array2 [CONSTA] [CONSTB])
{
for (int i = 0; i<CONST1; ++i)
{
for (int j = 0; j<CONST2; ++j)
{
...
}
}
}
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.

Make the two constants global, there's no harm in that. Leave the array
local to main.

john
 
J

JR

John Harrison said:
JR said:
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)

ITYM

int test(double array2 [3] [2], const int CONST1, const int CONST2)

It's not legal to miss out the int.
{
int returnValue;
for (int i = 0; i<=CONST1; ++i)
{
for (int j = 0; j<=CONST2; ++j)
{

These loops are wrong

for (int i = 0; i<CONST1; ++i)
{
for (int j = 0; j<CONST2; ++j)

You were going past the end of the array.
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.

Declare the constants as global, then you don't need to pass them to the
function at all.

const int CONSTA = 2;
const int CONSTB = 3;

int main()
{
double array1 [CONSTA] [CONSTB] = {{1,2},{3,4},{5,6}};
test(array1);
return 0;
}

int test(double array2 [CONSTA] [CONSTB])
{
for (int i = 0; i<CONST1; ++i)
{
for (int j = 0; j<CONST2; ++j)
{
...
}
}
}
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.

Make the two constants global, there's no harm in that. Leave the array
local to main.

john

Thank you both Rob and John. It appears that Visual C++ allows me to
use these constants without declaring their type. I will do so anyway
as it is better.

I had it in my had that globals were bad bad bad. Perhaps I just
should have gone with the simple route as suggested and make them
globals.

Thank you both again.
 
J

John Harrison

I had it in my had that globals were bad bad bad. Perhaps I just
should have gone with the simple route as suggested and make them
globals.

Thank you both again.

Unrestricted use of global variables is definitely bad. I don't see any
problem with global constants.

In any case you cannot get what you want to work, using the method you are
using, without making the constants global.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top