Cast from (long double*) to (const double*)

F

ferran

How can I cast from (long double*) to (const double*)

I have tried:
const double* Value1 = (const double*)Value2;

The compiler does not complain but the actual results when I access
the const double* are incorrect.

Note that the (long double*) is a pointer to an array of long doubles.
 
J

John Harrison

ferran said:
How can I cast from (long double*) to (const double*)

I have tried:
const double* Value1 = (const double*)Value2;

Well you've answered your own question.
The compiler does not complain but the actual results when I access
the const double* are incorrect.

Not surprisingly.
Note that the (long double*) is a pointer to an array of long doubles.

I think the question you meant to ask is how to I *convert* an array of long
doubles to an array of doubles, casting is not helpful as you have already
found out.

The only way to allocate a new array of doubles and copy the long doubles
over to the new array one by one. Something like

double *Value1 = new double[N];
for (int i = 0; i < N; ++i)
Value1 = Value2;

john
 
D

David Harmon

On Sat, 10 Apr 2004 16:44:44 +0100 in comp.lang.c++, "John Harrison"
The only way to allocate a new array of doubles and copy the long doubles
over to the new array one by one. Something like

double *Value1 = new double[N];
for (int i = 0; i < N; ++i)
Value1 = Value2;


Surely there is something better than copying one by one.
std::vector<double> Value1( Value2, Value2+N );

Or at the worst
std::copy(Value2, Value2+N, Value1);

Why would you ever write a 'for' loop for that? I don't get it.
 
R

Rolf Magnus

ferran said:
How can I cast from (long double*) to (const double*)

I have tried:
const double* Value1 = (const double*)Value2;

The compiler does not complain but the actual results when I access
the const double* are incorrect.

The above cast is only appropriate if you have a long double* that
actually doesn't point to a long double, but to a double.
Note that the (long double*) is a pointer to an array of long doubles.

I guess you actually want to have an array of double. In this case you
have to create a copy of your array.
 
J

John Harrison

David Harmon said:
On Sat, 10 Apr 2004 16:44:44 +0100 in comp.lang.c++, "John Harrison"
The only way to allocate a new array of doubles and copy the long doubles
over to the new array one by one. Something like

double *Value1 = new double[N];
for (int i = 0; i < N; ++i)
Value1 = Value2;


Surely there is something better than copying one by one.
std::vector<double> Value1( Value2, Value2+N );

Or at the worst
std::copy(Value2, Value2+N, Value1);

Why would you ever write a 'for' loop for that? I don't get it.


Granted, but I was just trying to emphasise to the OP that there has to be
some work done to perform the conversion (he was trying to cast), whether
that's done with an explicit loop, or in the internals of std::vector
constructor or std::copy.

john
 
D

David Harmon

On Sat, 10 Apr 2004 18:24:06 +0100 in comp.lang.c++, "John Harrison"
Granted, but I was just trying to emphasise to the OP that there has to be
some work done to perform the conversion (he was trying to cast), whether
that's done with an explicit loop, or in the internals of std::vector
constructor or std::copy.

Good point; certainly the ultimate conversion from long double to double
must be done one at a time somewhere. Perhaps it could be done without
needing to store the whole set of results. We don't know how the
results are to be used, but perhaps some kind of adapter class might
avoid a copying step. I guess if the destination is hard coded to
require an array of double there is not much choice but to build one.
 
F

ferran

Thankyou guys, I have implement a 'FOR' loop in order to copy al the
values from the array of long doubles to one of const doubles, and it
works perfectly.
One last question, besides the looking style, is there any reason why
should I use instead of a 'FOR' loop any of the next statements (that
were mentioned)?

std::copy(LondDoubleArray, LondDoubleArray + N, ConstDoubleArray);
or
std::vector<double>ConstDoubleArray (LondDoubleArray, LondDoubleArray+
N);


Thanks a lot for your help, I was getting a bit crazy.
 
K

Kevin Goodsell

ferran said:
Thankyou guys, I have implement a 'FOR' loop in order to copy al the
values from the array of long doubles to one of const doubles, and it
works perfectly.
One last question, besides the looking style, is there any reason why
should I use instead of a 'FOR' loop any of the next statements (that
were mentioned)?

std::copy(LondDoubleArray, LondDoubleArray + N, ConstDoubleArray);

For something like this I can think of no good reason to use a 'for'
loop instead of std::copy.
or
std::vector<double>ConstDoubleArray (LondDoubleArray, LondDoubleArray+
N);

This is fine if a vector is what you want, and still preferable to the
'for' loop, in my opinion.

On the other hand, your std::copy version is not fine if
ConstDoubleArray is actually a vector. If it is an empty vector, you'd
want to pass std::back_inserter(ConstDoubleArray) as the third argument.
If it's a vector that you've already resized to N elements, you want
ConstDoubleArray.begin() as the third argument.

-Kevin
 
R

Rolf Magnus

Kevin said:
For something like this I can think of no good reason to use a 'for'
loop instead of std::copy.

The OP was asking for a reason to use std::copy instead of a 'for' loop,
not the other way round.
 
K

Kevin Goodsell

Rolf said:
Kevin Goodsell wrote:




The OP was asking for a reason to use std::copy instead of a 'for' loop,
not the other way round.

Yeah, it looks like I misread it.

The reasons are that they are shorter, simpler, easier to understand,
and harder to get wrong than the 'for' loop.

-Kevin
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top