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

Discussion in 'C++' started by ferran, Apr 10, 2004.

  1. ferran

    ferran Guest

    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.
     
    ferran, Apr 10, 2004
    #1
    1. Advertising

  2. "ferran" <> wrote in message
    news:...
    > 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
     
    John Harrison, Apr 10, 2004
    #2
    1. Advertising

  3. ferran

    David Harmon Guest

    On Sat, 10 Apr 2004 16:44:44 +0100 in comp.lang.c++, "John Harrison"
    <> wrote,
    >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.
     
    David Harmon, Apr 10, 2004
    #3
  4. ferran

    Rolf Magnus Guest

    ferran wrote:

    > 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.
     
    Rolf Magnus, Apr 10, 2004
    #4
  5. "David Harmon" <> wrote in message
    news:...
    > On Sat, 10 Apr 2004 16:44:44 +0100 in comp.lang.c++, "John Harrison"
    > <> wrote,
    > >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
     
    John Harrison, Apr 10, 2004
    #5
  6. ferran

    David Harmon Guest

    On Sat, 10 Apr 2004 18:24:06 +0100 in comp.lang.c++, "John Harrison"
    <> wrote,
    >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.
     
    David Harmon, Apr 10, 2004
    #6
  7. ferran

    ferran Guest

    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.
     
    ferran, Apr 11, 2004
    #7
  8. ferran wrote:

    > 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
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Apr 11, 2004
    #8
  9. ferran

    Rolf Magnus Guest

    Kevin Goodsell wrote:

    >> 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.


    The OP was asking for a reason to use std::copy instead of a 'for' loop,
    not the other way round.
     
    Rolf Magnus, Apr 12, 2004
    #9
  10. Rolf Magnus wrote:
    > Kevin Goodsell wrote:
    >
    >
    >>>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.

    >
    >
    > 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
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Apr 12, 2004
    #10
    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. Heiko Vogel
    Replies:
    3
    Views:
    598
    Method Man
    Sep 14, 2004
  2. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,245
    Peter Shaggy Haywood
    Sep 20, 2005
  3. jeff

    cast unsigned long to long

    jeff, Dec 27, 2005, in forum: C Programming
    Replies:
    10
    Views:
    594
    Jack Klein
    Dec 27, 2005
  4. Javier
    Replies:
    2
    Views:
    620
    James Kanze
    Sep 4, 2007
  5. veryhotsausage
    Replies:
    1
    Views:
    1,899
    veryhotsausage
    Jul 4, 2008
Loading...

Share This Page