Pointers

This is a discussion on Pointers within the C++ Programming forums, part of the General Programming Boards category; Hello, I understand at the first cout i get the address of the third element but i dont understand what ...

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

    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;
    }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    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?
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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
    790
    Thank you,

    and how come that its in inversed order but if you print out the chars they are not?
    Compiler MSVC++ 2013 with Code::Blocks.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21