Re: conversion to char*

Discussion in 'C++' started by Michiel Salters, Aug 1, 2003.

  1. Marc Schellens <> wrote in message news:<>...
    > >>I want to convert, an eg. long int* to a char*
    > >>But using a static_cast<char*>(...) the compiler complains.
    > >>Why?

    > >
    > > static_cast<typename>(expression) is valid only if "typename"
    > > can be converted implicitly to the same type of "expression",
    > > or vice versa. There's no implicit conversion between two
    > > different pointer types.
    > >
    > >
    > >>So I am using a reinterpret_cast<char*>(...)
    > >>I know that the long int* points to a valid memeory location.
    > >>Is the reinterpret cast then dangerous, ie.
    > >>can I assume that pointers to differnt types have the same length,
    > >>and all other properties?

    > >
    > >
    > > No, you can't.


    Different pointer types may have different representations.
    unsigned char* and void* are guaranteed to have a an universal
    representation though, so you can cast any poiter to both types
    without information loss.

    > So what to do?
    > But de facto (speaking of windows, x86 linux, apple, sun...)
    > I can at least?
    > (I have never seen pointers of different length for eg in C on
    > these platforms (within one program)...)
    > Additionally I know that the types involved are the arrays of
    > different integer type, floats and doubles.
    > Are the poiters for them at least same length?


    Probably, but what does that give you? The pointers certainly
    can have different alignment requirements. On the platforms
    you mention, 50% of the float* couldn't point to a double.
    You can use memcpy though, or casts to unsigned char*. Both
    float and double are so-called POD types, which can be read
    and written as if they were char arrays. This can be used
    only as a transfer format; the only way to use these bytes
    is by ensuring these bytes end up in suitably aligned memory
    ( e.g. allocated by malloc(), or another double resp. float
    object ). When you

    > If yes:
    > If I add complex<float> and complex<double> still?


    No, these types aren't PODs. You can "read" them by casting
    their addresses to unsigned char*, but you can't do anything
    useful with the result, you can't even use it to create
    a hash key. You certainly can't set a complex<> to a certain
    value by writing directly to the unsigned char[]
    representation.

    HTH,
    --
    Michiel Salters
     
    Michiel Salters, Aug 1, 2003
    #1
    1. Advertising

  2. Michiel Salters wrote:

    >
    >
    > Different pointer types may have different representations.
    > unsigned char* and void* are guaranteed to have a an universal
    > representation though, so you can cast any poiter to both types
    > without information loss.
    >


    You can cast any *object pointer* to either type without loss. Function
    pointers are a different matter.

    -Kevin
     
    Kevin Goodsell, Aug 1, 2003
    #2
    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. wwj
    Replies:
    7
    Views:
    574
  2. Tim Johansson
    Replies:
    15
    Views:
    8,386
    Christopher Benson-Manica
    Apr 8, 2004
  3. spoilsport
    Replies:
    5
    Views:
    3,848
    BartenderSam
    Mar 4, 2004
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,096
    Ian Collins
    May 9, 2006
  5. Perro Flaco

    const char* to char* conversion

    Perro Flaco, Jun 19, 2006, in forum: C++
    Replies:
    34
    Views:
    1,304
    Victor Bazarov
    Jun 21, 2006
Loading...

Share This Page