CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Threaded View

  1. #1
    Join Date
    Dec 2012
    Posts
    18

    Casting Pointers in C style?

    This is something I'm having allot of trouble with. Casting Pointers in C Programming. I don't want to move onto implicit casts until I have this down pat.

    I'm failing to understand how casting pointers works.
    I know the below program is pointless but I am just trying to understand it.

    The line
    Code:
    int *mnt = (int*)&flt;
    if I read this correctly passes the address of flt which has been converted to an int to the pointer mnt.

    1 - When I output mnt I get a garbage value, probably because the address of flt is then converted to a pointer and passed onto mnt as a value and then reinterpreted as a memory address. (that is the first part I don't understand)

    2 - - What exactly does the (int*) cast say? Does this mean that a pointer will be returned or an address will be returned. What does the fact that the * is inside the parenthesis mean?



    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
      float flt=  6.5;
    
      int *mnt = (int*)&flt;
      
      cout << mnt << endl; // outputs hex memory address
      cout << *mnt << endl; // outputs garbage value
      
    
    }
    Last edited by tomadom; September 5th, 2014 at 10:44 PM.

Tags for this Thread

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