Thread: Pointers

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Pointers

    Hello,

    I understand at the first cout i get the address of the third element
    but i dont understand what is this big number i get on the second cout.

    Code:
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
        char buf[5]="abcd";
    
        cout << ((unsigned int*)&buf[2]) << endl;   // 003AF6F5 the address
        cout << *((unsigned int*)&buf[2]) << endl; //  2281727075  ?
    
        return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Print it in hex, then look up hex pairs in your ASCII table.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    I already thought about it and in hex it gives 89006463.

    Ah ok so its in inversed order 63=c, 64=d, 00=this must be the terminating zero and 89 is just some garbage?

    Is that it?
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Correct.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you,

    and how come that its in inversed order but if you print out the chars they are not?
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  2. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  3. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  4. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  5. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM