Passing an array to a function using a constant/variable

Discussion in 'C++' started by JR, Sep 13, 2004.

  1. JR

    JR Guest

    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
     
    JR, Sep 13, 2004
    #1
    1. Advertising

  2. JR wrote in news: 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/
     
    Rob Williscroft, Sep 14, 2004
    #2
    1. Advertising

  3. "JR" <> wrote in message
    news:...
    > 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
     
    John Harrison, Sep 14, 2004
    #3
  4. JR

    JR Guest

    "John Harrison" <> wrote in message news:<>...
    > "JR" <> wrote in message
    > news:...
    > > 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.
     
    JR, Sep 15, 2004
    #4
  5. >
    > 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
     
    John Harrison, Sep 15, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Afshar Mohebbi
    Replies:
    5
    Views:
    6,140
    Afshar Mohebbi
    Jun 26, 2006
  2. Replies:
    13
    Views:
    13,023
    Kai-Uwe Bux
    Jan 22, 2007
  3. sinbad
    Replies:
    7
    Views:
    683
    sinbad
    Jun 19, 2008
  4. puzzlecracker
    Replies:
    1
    Views:
    288
    puzzlecracker
    Oct 1, 2008
  5. Fredxx
    Replies:
    3
    Views:
    660
    Martin Thompson
    Jul 15, 2009
Loading...

Share This Page