Pointers To Arrays in C++

Under Standing The concept of Pointer to array how the point any location in an array

#include<iostream.h>
void main()
{
int word[5]={2,6,4,7,8};
int *ptr;
ptr=word;

// For Values of array
cout<<" There are three way to print the value of this array "<<endl;
for(int i=0;i<5;i++)
{
cout<<endl<<" Value of word["<<i<<"] by simple index: "<<word[i];
cout<<endl<<" Value of word["<<i<<"] by *word:        "<<*(word+i);
cout<<endl<<" Value of word["<<i<<"] by *(ptr):       "<<*(ptr+i);
 }
// For Adresses of Array
cout<<"\n\t\tAdresses of Arrays\nThere are also three ways to find the adresses of this array"<<endl;
for(int k=0;k<5;k++)
{
cout<<endl<<" Adress of word["<<k<<"] by & operator   "<<&word[k];
cout<<endl<<" Adress of word["<<k<<"] by word:        "<<(word+k);
cout<<endl<<" Adress of word["<<k<<"] by (ptr):       "<<(ptr+k);
}
}

0 comments:

Post a Comment

My Instagram