Wierd Addresses

This is a discussion on Wierd Addresses within the C++ Programming forums, part of the General Programming Boards category; I'm posing this problem to you guys because it seems that the entire computer science department is stumped by the ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    2

    Wierd Addresses

    I'm posing this problem to you guys because it seems that the entire computer science department is stumped by the following.

    The arcatecture of the processor is SPARC, and it's running SunOS 5.8.

    here's the code:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    int main()
    {
            signed int * ptr1, * ptr2;
            signed int array1[10];
            ptr1 = array1;
            ptr2 = &array1[0];
    
            cout<<ptr1<<endl;
            cout<<ptr2<<endl;
    
            return 0;
    }
    here's the output:
    Code:
    bash-2.03$ ./a.out
    0xffbef988
    0xffbef988
    Why is this wierd? Well according to ANSI C++ when you declare an array the actually name of the array with out an index value, (e.g. array1) is supposed to be a pointer which points to array1[0]. The problem is that in an x86 system, the above code would return two different addresses, one address for the pointer of array1 and one address for the location of the contents of array[0]. Can anyone explain this?

    Thank you

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    On both of my computers which are x86, one debian and one XP, both addresses are the same.

  3. #3
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Prelude's a very smart lady:
    http://faq.cprogramming.com/cgi-bin/...&id=1073086407

    Namely:
    Rule 1:
    An array name in an expression (in contrast to a declaration) is
    treated by the compiler as a pointer to the first element of the array.
    It's displaying exactly what it should.

    EDIT:
    Prelude's a girl??? (Sorry, just thought I'd be the first to throw that one in).

    EDIT2:
    I think I may have misunderstood the problem. But still, as it stands, the only time you should get the address of a pointer is if you do something like:
    Code:
    int *B;
    printf("%x", &B); //C code displaying the hexidecimal value of the address of B
    EDIT3:
    I am unfamiliar with x86...and debian for that matter...and probably should have just not posted anything on this topic.
    Last edited by Epo; 09-26-2005 at 10:57 PM.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    2
    I suppose there is some confusion of my question so I will infact simplify my code.

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    int main()
    {
    
            signed int array1[10];
    
            cout<<&array1<<endl;
            cout<<&array1[0]<<endl;
    
            return 0;
    }
    I should have posted this to begin with. My Fault.

    The output is still the same, two equal addresses.

    Is it still supposed to be the same?
    Last edited by theseabass; 09-26-2005 at 11:11 PM.

  5. #5
    FOX
    Join Date
    May 2005
    Posts
    188
    Yes, but it will change if you do something like this instead:
    Code:
            signed int array1[10];
            signed int *array_ptr = array1;
    
            cout<<&array_ptr<<endl;
            cout<<&array_ptr[0]<<endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Numeric addresses for computers
    By great in forum C Programming
    Replies: 4
    Last Post: 08-23-2010, 11:53 AM
  2. Addresses and extracting the value stored at an address
    By donoe_85 in forum C Programming
    Replies: 4
    Last Post: 04-08-2009, 03:38 AM
  3. Addresses and pointers help...
    By GCNDoug in forum C Programming
    Replies: 13
    Last Post: 04-07-2007, 05:37 PM
  4. OpenGL Wierd **** - Order of Drawing Stuff?
    By Tonto in forum Game Programming
    Replies: 9
    Last Post: 11-09-2006, 08:56 PM
  5. Wierd chars...
    By mikahell in forum C++ Programming
    Replies: 8
    Last Post: 08-06-2006, 07:18 PM

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