ch3-ArrayAddresses in hex and decimal in C/C++

Chapter_3     Exercise_3-18     StructArray Exercise_3-19







ArrayAddresses     TCP1, p. 198  (arrayaddresses.c,  ArrayAddresses.cpp)




arrayaddresses.c         download


#include <stdio.h> // for printf()

int main()
{
int i, a[10];

printf("sizeof(int) = %lu\n", sizeof(int));

for(i = 0; i < 10; i++)
{
printf("&a[%d] = %p (hex) = %ld (dec)\n", i, &a[i], (long)&a[i]);
}

return 0;
}

/*
gcc arrayaddresses.c -o arrayaddresses
./arrayaddresses
sizeof(int) = 4
&a[0] = 0x7ffc8a3b1ca0 (hex) = 140722627615904 (dec)
&a[1] = 0x7ffc8a3b1ca4 (hex) = 140722627615908 (dec)
&a[2] = 0x7ffc8a3b1ca8 (hex) = 140722627615912 (dec)
&a[3] = 0x7ffc8a3b1cac (hex) = 140722627615916 (dec)
&a[4] = 0x7ffc8a3b1cb0 (hex) = 140722627615920 (dec)
&a[5] = 0x7ffc8a3b1cb4 (hex) = 140722627615924 (dec)
&a[6] = 0x7ffc8a3b1cb8 (hex) = 140722627615928 (dec)
&a[7] = 0x7ffc8a3b1cbc (hex) = 140722627615932 (dec)
&a[8] = 0x7ffc8a3b1cc0 (hex) = 140722627615936 (dec)
&a[9] = 0x7ffc8a3b1cc4 (hex) = 140722627615940 (dec)
*/





Note:  See Hex_to_Decimal and Decimal_to_Hex on RapidTables, Numeric_conversions.











ArrayAddresses.cpp         download


#include <iostream>
using std::cout;
using std::endl;

int main()
{
int a[10];

cout << "sizeof(int) = "<< sizeof(int) << endl;

for(int i = 0; i < 10; i++)
{
cout << "&a[" << i << "] = " << &a[i] << " (hex) = "
<< (long)&a[i] << " (dec)" << endl;
}

return 0;
}

/*
g++ ArrayAddresses.cpp -o ArrayAddresses
./ArrayAddresses
sizeof(int) = 4
&a[0] = 0x7ffdfbd47720 (hex) = 140728828458784 (dec)
&a[1] = 0x7ffdfbd47724 (hex) = 140728828458788 (dec)
&a[2] = 0x7ffdfbd47728 (hex) = 140728828458792 (dec)
&a[3] = 0x7ffdfbd4772c (hex) = 140728828458796 (dec)
&a[4] = 0x7ffdfbd47730 (hex) = 140728828458800 (dec)
&a[5] = 0x7ffdfbd47734 (hex) = 140728828458804 (dec)
&a[6] = 0x7ffdfbd47738 (hex) = 140728828458808 (dec)
&a[7] = 0x7ffdfbd4773c (hex) = 140728828458812 (dec)
&a[8] = 0x7ffdfbd47740 (hex) = 140728828458816 (dec)
&a[9] = 0x7ffdfbd47744 (hex) = 140728828458820 (dec)
*/









Chapter_3     Exercise_3-18     StructArray BACK_TO_TOP Exercise_3-19



Comments

Popular posts from this blog

Contents