Sign in to follow this  
Richard Geslot

convert pointer type

Recommended Posts

Richard Geslot    235
Hello everyone,

I'm wondering if it's possible to do this with Cpp :




void* myPointer;//declare a pointer to a void

sizeof(myPointer[0]); //here, this line won't compile because it's a void*

//here, I'm searching code that transforme void* to a DWORD* (for example)
//.... ???? .....

sizeof(myPointer[0]); //here, this line will compile and we will have
//sizeof(DWORD)=4






I don't know if it's possible to cast a pointer type to another with Cpp...
If you know how, I would be very happy to know that!

Thanks

Share this post


Link to post
Share on other sites
karwosts    840
You can't change the type of myPointer itself (you already declared it to be void), but you can static cast it to a dword pointer:


void* myPointer;//declare a pointer to a void

sizeof(myPointer[0]); //here, this line won't compile because it's a void*

//here, I'm searching code that transforme void* to a DWORD* (for example)
DWORD* myDwordPointer = static_cast<DWORD*>(myPointer);

sizeof(myDwordPointer[0]); //here, this line will compile and we will have
//sizeof(DWORD)=4


Share this post


Link to post
Share on other sites
Richard Geslot    235
Thank you for your reply

In my code, I don't see how I could deal without changing the type of myPointer itself

here is the idea of my code


void A(void** ptr,bool WORDptr)
{
//here, A decides if ptr is a WORD* or a DWORD*
}

void main()
{
void* myPointer;
bool WORDptr;
A(&myPointer,&WORDptr);

if ( WORDptr )
{
//here I would like to say:
//myPointer, you are now a WORD* myPointer;
}
else
{
//here I would like to say:
//myPointer, you are now a DWORD* myPointer;
}


//and now, I have my algo that don't care if it's a WORD* or DWORD*
//for example:
for(int i=0;i<100;i++)
{
myPointer[i] = 2;
}

}







But as you said, I think it's not a C++ idea... I think it's impossible and I will have have to make a different code..

Share this post


Link to post
Share on other sites
rip-off    10976
You cannot recover the pointers type programmatically, unless you store it somewhere. The solution is either to store the size (if that is all that is needed), or to avoid using void pointers.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this