Sunday, 29 April 2012

C++ Program to Print the Addresses of Elements of Array

#include <assert.h>
#include <iostream>
#include <iomanip>      

const int SIZE  = 10; 
char a[SIZE] = "012345678";   

int main(  )
{
    int i;  

    for (i = 0; i < SIZE; ++i) {
        std::cout << std::hex;  
        assert(i >= 0);
        assert(i < sizeof(a)/sizeof(a[0]));
        std::cout << 
            "&a[i]=0x" <<  
                reinterpret_cast<int>(&a[i]) << 

            " (a+i)=0x" << 
                reinterpret_cast<int>(a+i) << 

            " a[i]=0x" <<  
                static_cast<int>(a[i]) << '\n',
        std::cout << std::dec; 
    }
    return (0);
}

No comments:

Post a Comment