Jump to content
  • Advertisement
Sign in to follow this  
RichardGe

convert pointer type

This topic is 2733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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 = 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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!