|
-
May 5th, 2009, 04:24 AM
#1
integer pointer conversion
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?
-
May 5th, 2009, 04:58 AM
#2
Re: integer pointer conversion
Code:
unsigned char c = 0x05;
int *a = reinterpret_cast<int*>( &c );
- Guido
-
May 5th, 2009, 06:27 AM
#3
Re: integer pointer conversion
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;
-
May 5th, 2009, 09:56 AM
#4
Re: integer pointer conversion
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
Code:
int* a = reinterpret_cast<int*>(0x05);
But that's also something you shouldn't really mess with unless you know what you're doing...
-
May 5th, 2009, 01:19 PM
#5
Re: integer pointer conversion
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.
-
May 5th, 2009, 01:26 PM
#6
Re: integer pointer conversion
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.
-
May 5th, 2009, 01:28 PM
#7
Re: integer pointer conversion
 Originally Posted by ninja9578
A char is 8 bytes, while a pointer is (mostly) 64 bytes, one Windows they may be 32 bytes depending on the version.
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.)
-
May 6th, 2009, 11:01 AM
#8
Re: integer pointer conversion
 Originally Posted by phenixa
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?
types dont have addressess so you can't take the address of 'int' etc.
-
May 6th, 2009, 11:15 AM
#9
Re: integer pointer conversion
 Originally Posted by Amleto
types dont have addressess so you can't take the address of 'int' etc.
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.
-
May 6th, 2009, 12:35 PM
#10
Re: integer pointer conversion
 Originally Posted by Lindley
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.
True. I assumed he was using a common OS (OSX, Windows, Desktop Linux, Solaris...) out of them, the only one that is 32 bit is Windows.
 Originally Posted by laserlight
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.)
Oops, yup, 64bit, not byte :P
-
May 6th, 2009, 12:38 PM
#11
Re: integer pointer conversion
There are 64-bit versions of Windows available.
-
May 7th, 2009, 12:23 AM
#12
Re: integer pointer conversion
 Originally Posted by laserlight
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.
why does that temporary not have an address then? is it because it is an rvalue (if that is the case)? would assigning it to an lvalue first resolve the problem in this case?
-
May 7th, 2009, 12:50 AM
#13
Re: integer pointer conversion
 Originally Posted by Amleto
is it because it is an rvalue (if that is the case)?
Yes.
 Originally Posted by Amleto
would assigning it to an lvalue first resolve the problem in this case?
Yes, and that is pretty much _Superman_'s example in post #3.
-
May 7th, 2009, 03:07 AM
#14
Re: integer pointer conversion
thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|