CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jun 2008
    Posts
    13

    Question 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?

  2. #2
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: integer pointer conversion

    Code:
    unsigned char c = 0x05;
    
    int *a = reinterpret_cast<int*>( &c );
    - Guido

  3. #3
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    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;
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  4. #4
    Join Date
    Aug 2007
    Posts
    858

    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...

  5. #5
    Join Date
    Jan 2009
    Posts
    1,689

    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.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: integer pointer conversion

    Quote 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.)
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Apr 2008
    Posts
    725

    Re: integer pointer conversion

    Quote Originally Posted by phenixa View Post
    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: integer pointer conversion

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Jan 2009
    Posts
    1,689

    Re: integer pointer conversion

    Quote Originally Posted by Lindley View Post
    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.

    Quote Originally Posted by laserlight View Post
    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

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: integer pointer conversion

    There are 64-bit versions of Windows available.

  12. #12
    Join Date
    Apr 2008
    Posts
    725

    Re: integer pointer conversion

    Quote Originally Posted by laserlight View Post
    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?

  13. #13
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: integer pointer conversion

    Quote Originally Posted by Amleto
    is it because it is an rvalue (if that is the case)?
    Yes.

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  14. #14
    Join Date
    Apr 2008
    Posts
    725

    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
  •  





Click Here to Expand Forum to Full Width

Featured