C PROGRAMS : POINTERS

C PROGRAMS : POINTERS
6. Program to find maximum element of an array of elements using pointer
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring array variable "a" of size 10, i=to iterate loop, max=maximum element
 int a[10], i, max;
 
 // Declaring pointer variable pointing to the the base address i.e ( a[0] )of array 'a'  
 int *ptr=a; // It is equivalent to : int *ptr; ptr=&a;
 
 // Inputing array elements
 printf("Enter 10 elements in an array : ");
 for(i=0;i<10;i++)
 {
  scanf("%d",&a[i]);
 }
 
 // Assigning first element to max, ptr points to second element after post increment
 max=*ptr++;
 
 // Determining maximum value
 for(i=0;i<10;i++)
 {
  if(*ptr > max)
  {
   max=*ptr;
   ptr++;
  }
 }
 
 // Printing maximum value
 printf("\nMaximum value : %d",max);
 
 getch(); // Linux user - Remove this

 return 0;
}

7. Program to print sum of all the elements in 2D array using pointer
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring 2D array and initializing it
 int a[3][4]={{3,4,5,6},{5,6,7,8},{8,9,10,11}};
 
 // Declaring varaible sum
 int sum=0;
 
 // Declaring pointer variable
 int *ptr, *ptre;
 
 ptr=&a[0][0]; // points to address of first element
 ptre=ptr+12; 
 
 // Calculating sum
 while(ptr<ptre)
 {
  sum+=*ptr++;
 }
 
 // Displaying calculated sum
 printf("Sum of all elements : %d",sum);
 
 getch(); // Linux user - Remove this

 return 0;
}

8. Program to sort an array of elements using pointers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{

 // Declaring array and initializing it
 int a[]={5,6,3,4,5};

 // Declaring pointer variable *p, *q and temp=tempeorary that helps in swapping numbers
 int *p,*q,temp;

 // Declaring variable i, j to iterate loop
 int i,j;

 //Displaying array elements
 printf("Array elements : ");
 for(i=0;i<5;i++)
 {
  printf("%d ",a[i]);
 }
 printf("\n");
 
 // Pointing to the address of first element
 p=&a[0];
 
 // Performing sorting
 for(i=0;i<5;i++)
 {
  for(j=i+1;j<5;j++)
  {
   // Swapping elements
   if(*(p+i) > *(p+j))
   {
    temp=*(p+i);
    *(p+i)=*(p+j);
    *(p+j)=temp;
   }
  }
 }

 // Displaying sorted elements
 printf("Sorted elements : ");
 for(i=0;i<5;i++)
 {
  printf("%d ",a[i]);
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

9. Program to sort string using pointers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str
 char str[50], *p;
 
 // Declaring variable i, j to iterate loop
 int i, j;
 
 // Declaring variable l=length, temp=tempeorary that helps in swapping
 int l=0, temp;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 p=&str[0];
 
 // finding length
 while(str[l]!='\0')
 l++;
 
 // Performing sorting
 for(i=0;i<l;i++)
 {
  for(j=i+1;j<l;j++)
  {
   // Swapping characters
   if(*(p+i)>*(p+j))
   {
    temp=*(p+i);
    *(p+i)=*(p+j);
    *(p+j)=temp;
   }
  }
 }
 
 // Displaying sorted string
 printf("Sorted string : \n");
 p=&str[0];
 
 for(i=0;i<l;i++)
 {
  printf("%c\n",*(p+i));
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

10. Program to search given element in an array using pointers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{

 // Declaring array and initializing it, *p=points to the address of first element
 int a[]={5,6,3,4,5}, *p;
 p=&a[0];
 
 // Declaring variable i = to iterate loop
 int i;
 
 /* Declaring variable es = element to be searched, flag = to indicate 
 if element is present or not */
 int es, flag=0;
 
 // Displaying array element
 printf("Array elements : ");
 for(i=0;i<5;i++)
 {
  printf("%d ",*(p+i));
 }
 
 // Inputing element to be searched
 printf("\nEnter element to be searched : ");
 scanf("%d",&es);
 
 // Searching element
 for(i=0;i<5;i++)
 {
  if(*(p+i)==es)
  {
   flag=1;
   break;
  }
 }
 
 // Displaying whether element found or not
 if(flag==1)
 printf("Element found ");
 
 else
 printf("Element not found ");
 
 getch(); // Linux user - Remove this

 return 0;
}

11. Program to demonstrate the use of pointer to pointer
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 int x,*p,**q;
             
    printf("\nEnter a number : ");
    scanf("%d",&x);
    
    p=&x;
    q=&p;
    
    printf("\nValue of x=%d",x);
    printf("\nValue of x through pointer=%d",*p);
    printf("\nVal. of x through pointer to  pointer=%d",**q);
    printf("\nValue of p=%u",p);
    printf("\nValue of q=%u",q);
    printf("\nAddres of p=%u",q);
     
 getch(); // Linux user - Remove this

 return 0;
}

Share this

Related Posts

Previous
Next Post »