473,387 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Passing an array to a function using a constant/variable

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
Jul 22 '05 #1
4 2127
JR wrote in news:8b*************************@posting.google.co m 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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

"JR" <JR*****@email.com> wrote in message
news:8b*************************@posting.google.co m...
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
Jul 22 '05 #3
JR
"John Harrison" <jo*************@hotmail.com> wrote in message news:<2q*************@uni-berlin.de>...
"JR" <JR*****@email.com> wrote in message
news:8b*************************@posting.google.co m...
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.
Jul 22 '05 #4
>
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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John MacIntyre | last post by:
Hi, I have a page with a series of child pages loaded into an iframe. When I move from page to page, I store an object containing the child's control data in a variable on the main page, then...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
by: Gerald | last post by:
I have a problem with an array of pointers. In a program I'm writing, I have to read a file, containing thousands of short lines. The content of another file will be compared against each line...
16
by: herbertF | last post by:
Hi guys, In a program (not my own) I encountered the declaration of a constant pointer to an array consisting of two other const pointers to arrays. Not quite sure why they do it so complicated,...
5
by: Michael | last post by:
Hi, once I read here that it is not 'a good idea' to pass variables that are not initialized to a function. I have void something ( double *vector ); ....
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
22
by: howachen | last post by:
which one is recommend? seems they perform the same thing... regards, howa
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.