Why can't I static_cast to a pointer type?

J

Jeff Schwab

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
 
R

Rob Williscroft

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

try:

reinterpret_cast< 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.
Fails to compile with g++; message is:
[snip]

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?

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

Jeff Schwab

Rob said:
Jeff Schwab wrote in



try:

reinterpret_cast< char const* const >( 0xFFFFFFFF );


Bingo! Thanks, Rob.
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.

Fails to compile with g++; message is:

[snip]

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?


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.
 

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,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top