Tuesday, 3 April 2018

Pointer in Arrays

Consider the declaration
int b[5];
int *ptr;
The pointer to array is given as below
ptr=&b[0];
which is same as
ptr=b;
If the pointer is incremented to the next data elements , then address of the incremented value of the pointer will be same as the value of the next element.

ptr=&b[0]            *ptr=b[0]
ptr+1=&b[1]        *(ptr+1)=b[1]
ptr+2=&b[2]        *(ptr+2)=b[2]
ptr+3=&b[3]        *(ptr+3)=b[3]

The expression-
  value[i] is same as (value+i)
that is
b[i] ==*(b+i)
b[0] ==*(b+0)
b[1] ==*(b+1)
b[2] ==*(b+2)
b[3] ==*(b+3)

WAP to initialize and display five elements from array by using pointer to array.

#include<conio.h>
#include<iostream.h>
void main( )
{
int a[5]={10,20,30,40,50};
int *ptr, i;
clrscr( );
ptr=&a[0];
cout<<"\n\n\tElements in array are--->";
for(i=0;i<5;i++)
{
cout<<" "<<*ptr;
ptr++;
}
getch( );
}


Output :-
Element in array are---> 10 20 30 40 50

WAP to search  an element from array using pointers to array

#include<conio.h>
#include<iostream.h>
void main( )
{
int a[5];
int *ptr, i, f=0, item;
clrscr( );
ptr=&a[0];
cout<<"\n\n\tEnter Five elements";
for(i=0;i<5;i++)
{
cin>>*ptr;
ptr++;
}
cout<<"\n\n\tEnter Element to search";
cin>>item;
ptr=&a[0];
for(i=0;i<5;i++)
{
if(*ptr==item)
{
cout<<"\n\n\tElement is searched in array at"<<" "<<i+1<<"th location";
f=1;
break;
}
ptr++;
}
if(f==0)
cout<<"\n\n\tElement is not searched in array";
getch( );
}

Output:-
Enter Five element 12 23 34 45 56

Enter Element to search 56

Element is searched in array at 5 th location

WAP to insert an element at location of array by using pointer to array.

#include<conio.h>
#include<iostream.h>
void main( )
{
int a[10], i, *ptr1,n,loc,ele;
clrscr( );
cout<<"\n\n\tEnter Size of the Array";
cin>>n;
ptr1=&a[0];
cout<<"\n\n\tEnter Element in the Array";
for(i=0;i<n;i++)
{
cin>>*ptr1;
ptr1++;
}
cout<<"\n\n\tEnter Location to insert in to array";
cin>>loc;
cout<<"\n\n\tEnter Element to insert into Array";
cin>>ele;
ptr1--;
for(i=n-1;i>=0;i--)
{
if(i==loc-1)
{
*(ptr1+1)=*ptr1;
*ptr1=ele;
}
*(ptr1+1)=*ptr1;
ptr1--;
}
ptr1=&a[0];
cout<<"\n\n\tElement in the Array after Insertion--->";
for(i=0;i<n+1;i++)
{
cout<<" "<<*ptr1;
ptr1++;
}
getch( );
}

Output:-
Enter Size of the Array 5
Enter Element in the Array 12 23 34 45 56
Enter Location to insert in to array 3
Enter Element to insert into Array 90
Element in the Array after Insertion is---> 12 12 23 90 45 56

WAP to delete an element from an array from the specified location using pointers to array.

#include<conio.h>
#include<iostream.h>
void main( )
{
int a[10],i,*ptr1,n,loc;
clrscr( );
cout<<"\n\n\tEnter Size of the Array";
cin>>n;
ptr1=&a[0];
cout<<"\n\n\t Enter the Elements in the Array";
for(i=0;i<n;i++)
{
cin>>*ptr1;
ptr1++;
}
cout<<"\n\n\t Enter the Location of element to be deleted";
cin>>loc;
ptr1=&a[loc];
for(i=loc-1;i<n;i++)
{
*ptr1=*(ptr1+1);
ptr1++;
}
ptr1=&a[0];
cout<<"\n\n\tElements in Array after deletion is--->";
for(i=0;i<n-1;i++)
{
cout<<" "<<*ptr1;
ptr1++;
}
getch( );
}

Output:-
Enter Size of the Array 5
Enter the Elements in the Array 12 23 34 45 56
Enter the location of element to be deleted 3
Elements in Array after deletion is---> 12 23 34 56

0 Comments:

Post a Comment

Popular Posts

Categories

Android (21) AngularJS (1) Books (3) C (75) C++ (81) Data Strucures (4) Engineering (13) FPL (17) HTML&CSS (38) IS (25) Java (85) PHP (20) Python (83) R (68) Selenium Webdriver (2) Software (13) SQL (27)