Displaying memory address of the array elements in C

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info:

To do: Displaying memory address of the array elements in C programming

To show: The pointers and elements of arrays of different data types

 

 

// Demonstrates the relationship between memory addresses (pointers) and elements of arrays of different data types

#include <stdio.h>

 

void main(void)

{

// declare three arrays and a counter variable

int i[10], x;

float f[10];

double d[10];

 

// print the table heading

printf("\nArray's el. add of i[x] add of f[x] add of d[x]");

printf("\n|================================");

printf("======================|");

// print the addresses of each array element

for(x=0; x<10; x++)

printf("\nElement %d:\t%p\t%p\t%p",x,&i[x],&f[x],&d[x]);

printf("\n|================================");

printf("======================|\n");

printf("\nLegends:");

printf("\nel.- element, add - address\n");

printf("\ndifferent pc, shows different addresses\n");

}

 

Output example:

 

Array's el. add of i[x] add of f[x] add of d[x]

|======================================================|

Element 0: 0012FF3C 0012FF00 0012FEA8

Element 1: 0012FF40 0012FF04 0012FEB0

Element 2: 0012FF44 0012FF08 0012FEB8

Element 3: 0012FF48 0012FF0C 0012FEC0

Element 4: 0012FF4C 0012FF10 0012FEC8

Element 5: 0012FF50 0012FF14 0012FED0

Element 6: 0012FF54 0012FF18 0012FED8

Element 7: 0012FF58 0012FF1C 0012FEE0

Element 8: 0012FF5C 0012FF20 0012FEE8

Element 9: 0012FF60 0012FF24 0012FEF0

|======================================================|

Legends:

el.- element, add - address

different pc, shows different addresses

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index