Storing address of pointer (cross platform)

H

howa

In 32 bit pc, I can write

e.g.

char* p1 = "apple";
cout<<(int)p1;


In 64 bit pc, I need to use

char* p1 = "apple";
cout<<(__int64)p1;


What are the cross platform way to so?

Thanks.
 
E

Erik Wikström

In 32 bit pc, I can write

e.g.

char* p1 = "apple";
cout<<(int)p1;


In 64 bit pc, I need to use

char* p1 = "apple";
cout<<(__int64)p1;


What are the cross platform way to so?

If your platforms have C compilers providing <stdint.h> you can check if
they define the typedef intptr_r or uintptr_t, they should be large
enough for a void pointer. If you do not have those typedefs you need to
provide them (or similar) on your own.
 
H

howa

If your platforms have C compilers providing <stdint.h> you can check if
they define the typedef intptr_r or uintptr_t, they should be large
enough for a void pointer. If you do not have those typedefs you need to
provide them (or similar) on your own.

Thanks.
 
T

Tomás Ó hÉilidhe

howa:
In 32 bit pc, I can write

e.g.

char* p1 = "apple";
cout<<(int)p1;


In 64 bit pc, I need to use

char* p1 = "apple";
cout<<(__int64)p1;


What are the cross platform way to so?


I'm not sure if cout can print a pointer, but printf certainly can:

int i;

printf("%p",(void*)&i);
 
R

Rolf Magnus

howa said:
In 32 bit pc, I can write

e.g.

char* p1 = "apple";
cout<<(int)p1;


In 64 bit pc, I need to use

char* p1 = "apple";
cout<<(__int64)p1;


What are the cross platform way to so?

cout << static_cast<void*>(p1);
 
J

James Kanze

In 32 bit pc, I can write

char* p1 = "apple";
cout<<(int)p1;
In 64 bit pc, I need to use
char* p1 = "apple";
cout<<(__int64)p1;

(A more portable 64 bit type would be long long.)
What are the cross platform way to so?

cout << static_cast< void* >( p1 ) ;

It's the only way guaranteed to work everywhere.

It doesn't allow you to control the output format in any way,
however. If you need to control the output format, something
like:
cout << static_cast< uintptr_t >( p1 ) ;
will usually work (but uintptr_t is not guaranteed to be present
on a machine where pointers are larger than 64 bits).

If you have to work with an older compiler, which doesn't
support the new integer types, then casting to size_t is often
OK (although I've used platforms where it wouldn't work).
 

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,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top