Page 49 - DCAP407_DATA_STRUCTURE
P. 49
Data Structure
Let us consider an example wherein we declare an array, access the array using pointers, and print the
array.
#include<stdio.h>
int i;
void printarr(int b[])
{
for(i=0;i<5;i++) //Iterations using for loop
{
printf("Value in the array is %d\n",b[i]); //Printing the values in the
array
}
}
void printdetail(int b[])
{
for(i=0;i<5;i++) //Iterations using for loop
{
/*Printing the values and addresses of the array elements*/
printf("value in array is %d and its address is %8u\n", b[i],&b[i]);
}
}
void main()
{
int b[5]; //Declaring the array
clrscr();
for(i=0;i<5;i++) //Iterations using for loop
{
b[i]=i; //Assign the value of i to each array element
}
printarr(b); //Call the printarr function
printdetail(b); //Call the printdetail function
getch();
}
Output:
Value in the array is 0
Value in the array is 1
Value in the array is 2
Value in the array is 3
Value in the array is 4
Value in the array is 0 and its address is 65516
Value in the array is 1 and its address is 65518
Value in the array is 2 and its address is 65520
Value in the array is 3 and its address is 65522
Value in the array is 4 and its address is 65524
42 LOVELY PROFESSIONAL UNIVERSITY