Passing array by reference

J

john

Guys,

quick question - got a function like:

void CalculateApproach(float (&Params)[4], double (&ObjectPos)[3],
double (&VehiclePos)[4], double (&NewPos)[4])


OK, the seconds argument should come from an array but this array is
bigger than the expected 3 i.e it has 10 elements but I only need 4,5
and 6 so I tried something like:

CalculateApproach(fAppParams, &dFeatures[4], dCurVehPos, dNewVehPos);

but it fails for me. Am I trying to do something that is not allowed and
hence should just make new array[3] and copy my values in and then pass
it into the function or can someone give me hint towards the syntax i
should be using if it can work?



Cheers,


John.
 
V

Victor Bazarov

john said:
Guys,

quick question - got a function like:

void CalculateApproach(float (&Params)[4], double (&ObjectPos)[3],
double (&VehiclePos)[4], double (&NewPos)[4])


OK, the seconds argument should come from an array but this array is
bigger than the expected 3 i.e it has 10 elements but I only need 4,5
and 6 so I tried something like:

CalculateApproach(fAppParams, &dFeatures[4], dCurVehPos, dNewVehPos);

but it fails for me. Am I trying to do something that is not allowed

Yes, essentially.
and hence should just make new array[3] and copy my values in and
then pass it into the function or can someone give me hint towards
the syntax i should be using if it can work?

You could try forcing it with a cast...

void foo(int (&ar)[3])
{
ar[0] = 1;
ar[1] = 11;
ar[2] = 111;
}
#include <cassert>
int main()
{
typedef int (*pArrayOf3int)[3];
int array[10] = {};
foo(*pArrayOf3int(array + 5));
assert(array[5] == 1);
assert(array[6] == 11);
assert(array[7] == 111);
}

V
 
C

Carl Barron

john said:
Guys,

quick question - got a function like:

void CalculateApproach(float (&Params)[4], double (&ObjectPos)[3],
double (&VehiclePos)[4], double (&NewPos)[4])


OK, the seconds argument should come from an array but this array is
bigger than the expected 3 i.e it has 10 elements but I only need 4,5
and 6 so I tried something like:

CalculateApproach(fAppParams, &dFeatures[4], dCurVehPos, dNewVehPos);

but it fails for me. Am I trying to do something that is not allowed and
hence should just make new array[3] and copy my values in and then pass
it into the function or can someone give me hint towards the syntax i
should be using if it can work?
typedef double DoubleFour[4];
typedef double DoubleThree[3];

void CalculateApproach(DoubleFour &,DoubleThree &,DoubleFour
&,DoubleFor &);

CalculateApproach(fAppParams, (DoubleThree &)(dFeatures[4]),
dCurVehPOs,dNewVehPos);

this should work, as an array must be decayable to a pointer so the
cast is only to tell the compiler what is going on. A reinterpret_cast
will be needed and this is a strong warning that it might not be
portable.
 
J

Jim Langston

john said:
Guys,

quick question - got a function like:

void CalculateApproach(float (&Params)[4], double (&ObjectPos)[3], double
(&VehiclePos)[4], double (&NewPos)[4])


OK, the seconds argument should come from an array but this array is
bigger than the expected 3 i.e it has 10 elements but I only need 4,5 and
6 so I tried something like:

CalculateApproach(fAppParams, &dFeatures[4], dCurVehPos, dNewVehPos);

Calculateapproache( float&* fAppParams, double& dFeatures[4], double&*
dcurVehPos, double&* dNewVehPos )
but it fails for me. Am I trying to do something that is not allowed and
hence should just make new array[3] and copy my values in and then pass it
into the function or can someone give me hint towards the syntax i should
be using if it can work?



Cheers,


John.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:[email protected] ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
 
V

Victor Bazarov

Jim said:
john said:
Guys,

quick question - got a function like:

void CalculateApproach(float (&Params)[4], double (&ObjectPos)[3],
double (&VehiclePos)[4], double (&NewPos)[4])


OK, the seconds argument should come from an array but this array is
bigger than the expected 3 i.e it has 10 elements but I only need
4,5 and 6 so I tried something like:

CalculateApproach(fAppParams, &dFeatures[4], dCurVehPos, dNewVehPos);

Calculateapproache( float&* fAppParams, double& dFeatures[4], double&*
dcurVehPos, double&* dNewVehPos )

The OP's line looks like a function call, and yours looks like
a declaration. Are you sure that's what he's asking about?

V
 
J

john

john said:
Guys,

quick question - got a function like:

void CalculateApproach(float (&Params)[4], double (&ObjectPos)[3],
double (&VehiclePos)[4], double (&NewPos)[4])


OK, the seconds argument should come from an array but this array is
bigger than the expected 3 i.e it has 10 elements but I only need 4,5
and 6 so I tried something like:

CalculateApproach(fAppParams, &dFeatures[4], dCurVehPos, dNewVehPos);

but it fails for me. Am I trying to do something that is not allowed and
hence should just make new array[3] and copy my values in and then pass
it into the function or can someone give me hint towards the syntax i
should be using if it can work?



Cheers,


John.


Thanks guys, the cast worked. I thought I could use a cast but was
unsure about the syntax but using the typedef sorted it!

Thanks again.


John.
 
J

Jim Langston

Victor Bazarov said:
Jim said:
john said:
Guys,

quick question - got a function like:

void CalculateApproach(float (&Params)[4], double (&ObjectPos)[3],
double (&VehiclePos)[4], double (&NewPos)[4])


OK, the seconds argument should come from an array but this array is
bigger than the expected 3 i.e it has 10 elements but I only need
4,5 and 6 so I tried something like:

CalculateApproach(fAppParams, &dFeatures[4], dCurVehPos, dNewVehPos);

Calculateapproache( float&* fAppParams, double& dFeatures[4], double&*
dcurVehPos, double&* dNewVehPos )

The OP's line looks like a function call, and yours looks like
a declaration. Are you sure that's what he's asking about?

Actually, no. And re-reading I think I answered a question he didn't ask.
 
M

Mathias Gaunard

void CalculateApproach(float (&Params)[4], double (&ObjectPos)[3],
double (&VehiclePos)[4], double (&NewPos)[4])

OK, the seconds argument should come from an array but this array is
bigger than the expected 3 i.e it has 10 elements but I only need 4,5
and 6 so I tried something like:

CalculateApproach(fAppParams, &dFeatures[4], dCurVehPos, dNewVehPos);

but it fails for me. Am I trying to do something that is not allowed

Yes.
The type of &dFeatures[4] is double*, with certainly isn't an array of
three doubles or a reference to one, but only a pointer to a double.
and
hence should just make new array[3] and copy my values in and then pass
it into the function

That's a possible solution.

or can someone give me hint towards the syntax i
should be using if it can work?

You could use some reinterpret_casts, but that's evil too.
The correct way would probably be to design your function differently.
 

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
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top