hi,
is there any way to convert unsigned char to int* on the fly?
like this
unsigned char c = 0x05;
int *a = &(int) c;
this code give me "Must take address of a memory location" error
any ideas?
Printable View
hi,
is there any way to convert unsigned char to int* on the fly?
like this
unsigned char c = 0x05;
int *a = &(int) c;
this code give me "Must take address of a memory location" error
any ideas?
Code:unsigned char c = 0x05;
int *a = reinterpret_cast<int*>( &c );
I'm sure that will produce unexpected results because when you access the value in the pointer a it may not be 0x05.
If you expect the value to be 5 you should use an intermediate int variable.
Code:unsigned char c = 0x05;
int a = c;
int* b = &a;
Do you want the pointer to point to "c", or to the address 0x05?
GNiewerth showed you how to do the former, but I don't see it being very useful. Trying to dereference the int pointer will give you trash, because it's reading 3 bytes in addition to "c". If you try to assign to it, you're going to corrupt the stack.
To do the latter, just do
But that's also something you shouldn't really mess with unless you know what you're doing...Code:int* a = reinterpret_cast<int*>(0x05);
I'm not sure what you're trying to accomplish here. You can't ever store a pointer in a char. A char is 8 bytes, while a pointer is (mostly) 64 bytes, one Windows they may be 32 bytes depending on the version.
The size of a pointer depends only on whether you're on a 64- or 32-bit OS. There are other types which vary between Windows/Linux/OSX, but not pointers.
I think you mean bits rather than bytes, but of course the actual values are implementation defined, within the required limits. (Consequently, if the sizeof a pointer is 1, I suppose you could store a pointer in a char without loss of information.)Quote:
Originally Posted by ninja9578
Ah, but that does not attempt to take the address of the int type. That attempts to take the address of a temporary that is the result of a cast from unsigned char to int.Quote:
Originally Posted by Amleto
There are 64-bit versions of Windows available.
Yes.Quote:
Originally Posted by Amleto
Yes, and that is pretty much _Superman_'s example in post #3.Quote:
Originally Posted by Amleto
thanks :)