Why can't I static_cast to a pointer type?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Schwab

    Why can't I static_cast to a pointer type?

    int main()
    {
    char const* const p =
    static_cast< char const* const >( 0xffffffff );
    }


    Fails to compile with g++; message is:

    g++ -c -o main.o main.cc
    main.cc: In function `int main()':
    main.cc:3: error: invalid static_cast from type `unsigned int' to type
    `const
    char* const'
    make: *** [main.o] Error 1


    Why doesn't this work? How can one write to specific addresses? For
    example, Lupher Cypher posted a C-style cast to "char* far", in an
    attempt to write to a text display. I'm willing to accept that "far"
    might be a necessary extension for some architectures, e.g. 16-bit chips
    in PDA's with 32 MB of RAM. On such an architecture, he may well need
    to write data directly to particular addresses in order to communicate
    with a memory-mapped device; how is he supposed to do it? Are the old
    C-style casts the only way?

    Special thanks to anyone with experience on this issue.

    -Jeff

  • Rob Williscroft

    #2
    Re: Why can't I static_cast to a pointer type?

    Jeff Schwab wrote in news:o5edncanwd dazXeiRVn-gw@comcast.com:
    [color=blue]
    > int main()
    > {
    > char const* const p =
    > static_cast< char const* const >( 0xffffffff );
    > }
    >[/color]

    try:

    reinterpret_cas t< char const* const >( 0xFFFFFFFF );


    Check the spelling though, its not very often I get to write this cast
    (making the cast ugly(tm) and thus difficult to spell (for the likes of
    me anyway :) was as I understand it intentional.

    How (and in what way its meanfull/usefull) is implemenation defined.

    But if you know the bit layout of your platform's pointers, it should
    do what you want.
    [color=blue]
    >
    > Fails to compile with g++; message is:
    >[/color]
    [snip][color=blue]
    >
    > Why doesn't this work? How can one write to specific addresses? For
    > example, Lupher Cypher posted a C-style cast to "char* far", in an
    > attempt to write to a text display. I'm willing to accept that "far"
    > might be a necessary extension for some architectures, e.g. 16-bit
    > chips in PDA's with 32 MB of RAM. On such an architecture, he may
    > well need to write data directly to particular addresses in order to
    > communicate with a memory-mapped device; how is he supposed to do it?
    > Are the old C-style casts the only way?
    >[/color]

    I would hope that the compilers/libraries that come with such systems
    would provide a mechanism to do this (worst case a macro maybe).


    Rob.
    --

    Comment

    • Jeff Schwab

      #3
      Re: Why can't I static_cast to a pointer type?

      Rob Williscroft wrote:[color=blue]
      > Jeff Schwab wrote in news:o5edncanwd dazXeiRVn-gw@comcast.com:
      >
      >[color=green]
      >>int main()
      >>{
      >> char const* const p =
      >> static_cast< char const* const >( 0xffffffff );
      >>}
      >>[/color]
      >
      >
      > try:
      >
      > reinterpret_cas t< char const* const >( 0xFFFFFFFF );[/color]


      Bingo! Thanks, Rob.
      [color=blue]
      > Check the spelling though, its not very often I get to write this cast
      > (making the cast ugly(tm) and thus difficult to spell (for the likes of
      > me anyway :) was as I understand it intentional.
      >
      > How (and in what way its meanfull/usefull) is implemenation defined.
      >
      > But if you know the bit layout of your platform's pointers, it should
      > do what you want.
      >
      >[color=green]
      >>Fails to compile with g++; message is:
      >>[/color]
      >
      > [snip]
      >[color=green]
      >>Why doesn't this work? How can one write to specific addresses? For
      >>example, Lupher Cypher posted a C-style cast to "char* far", in an
      >>attempt to write to a text display. I'm willing to accept that "far"
      >>might be a necessary extension for some architectures, e.g. 16-bit
      >>chips in PDA's with 32 MB of RAM. On such an architecture, he may
      >>well need to write data directly to particular addresses in order to
      >>communicate with a memory-mapped device; how is he supposed to do it?
      >>Are the old C-style casts the only way?
      >>[/color]
      >
      >
      > I would hope that the compilers/libraries that come with such systems
      > would provide a mechanism to do this (worst case a macro maybe).
      >
      >
      > Rob.[/color]

      Comment

      Working...