Topic: Passing array by reference
Author: reply@newsgroup.com (john)
Date: Wed, 18 Jul 2007 16:52:05 GMT Raw View
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.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Carl Barron <cbarron413@adelphia.net>
Date: Wed, 18 Jul 2007 20:35:06 CST Raw View
In article <XVqni.57$SI5.43@newsfe2-gui.ntli.net>, john
<reply@newsgroup.com> wrote:
> 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.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Jim Langston" <tazmaster@rocketmail.com>
Date: Thu, 19 Jul 2007 02:38:56 CST Raw View
"john" <reply@newsgroup.com> wrote in message
news:XVqni.57$SI5.43@newsfe2-gui.ntli.net...
> 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:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
>
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: reply@newsgroup.com (john)
Date: Thu, 19 Jul 2007 12:29:50 GMT Raw View
john wrote:
> 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.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Mathias Gaunard <loufoque@gmail.com>
Date: Thu, 19 Jul 2007 14:26:18 CST Raw View
On Jul 18, 6:52 pm, re...@newsgroup.com (john) wrote:
> 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.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]