Showing posts with label Pointers. Show all posts
Showing posts with label Pointers. Show all posts
C PROGRAMS : POINTERS

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;
}
C PROGRAMS : POINTERS

C PROGRAMS : POINTERS

C PROGRAMS : POINTERS
1. Program to accept two numbers and print its address along with the numbers
# include <stdio.h>
# include <conio.h> // Linux user - Remove this

int main( )
{
 // Declaring variable a, b = two numbers
 int a, b;
 
 // Inputing two numbers
 printf("Enter first number : ");
 scanf("%d",&a);
 
 printf("Enter first number : ");
 scanf("%d",&b);
 
 // Printing address along with number
 printf("First number : %d, Address : %d\n",a, &a);
 printf("First number : %d, Address : %d\n",*(&a), &a);
 
 printf("Second number : %d, Address : %d\n",b, &b);
 printf("Second number : %d, Address : %d",*(&b), &b);
 
 getch(); // Linux user - Remove this

 return 0;
}

2. Program to accept two numbers and print the sum of given two numbers by using pointers
# include <stdio.h>
# include <conio.h> // Linux user - Remove this

int main( )
{
 // Declaring variable a, b = two numbers
 int a, b;
 
 // Declaring variale s=sum
 int s=0;
 
 // Inputing two numbers
 printf("Enter first number : ");
 scanf("%d",&a);
 
 // Inputing second number
 printf("Enter first number : ");
 scanf("%d",&b);
 
 // Determining sum
 s=*(&a)+*(&b);
 
 // Printing sum
 printf("Sum = %d",s);
 
 getch( ); // Linux user - Remove this

 return 0;
}

3. Program to interchange two values using pointers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable a, b for two values, swap to interchange value
 int a, b, swap;
 
 // Declaring pointer that holds address of two values
 int *c, *d;
 
 // Storing address
 c=&a;
 d=&b;
 
 // Inputing values
 printf("Enter first value a : ");
 scanf("%d",&a);
 
 printf("Enter second value b : ");
 scanf("%d",&b);
 
 // Printing original value
 printf("Original value : a = %d, b = %d\n",a, b);
 
 // Interchanging value
 swap=*c;
 *c=*d;
 *d=swap;
 
 // Printing interchanged value
 printf("Interchanged value : a = %d, b = %d ",a, b);
  
 getch(); // Linux user - Remove this

 return 0;
}

4. Implement a function interchange() to interchange two values using pointers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable a, b for two values
 int a,b;

 // Inputing values
 printf("Enter first value a : ");
 scanf("%d",&a);
 
 printf("Enter second value b : ");
 scanf("%d",&b);
 
 // Printing original value
 printf("Original value : a = %d, b = %d\n",a, b);
 
 // Calling function interchange
 interchange(&a, &b);
 
 getch(); // Linux user - Remove this

 return 0;
}

void interchange(int *x, int *y)

{
 int swap;
 
 // Interchanging value
 swap=*x;
 *x=*y;
 *y=swap;
 
 // Printing interchanged value
 printf("Interchanged value : a = %d, b = %d ",*x, *y);
}


5. Program to sum all the elements of an array using pointer
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring array variable "a" of size 10 and initializing it
 int a[10]={21, -2, 4, 6, 9, 12, 43, 22, 4, 8}, sum=0;
 
 // Declaring pointer type 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;
 
 // Declaring variable ptre = stores address of 11th element
 int *ptre;
 ptre = ptr+10;
 
 // Calculating sum
 while(ptr<ptre)
 {
  sum+=*ptr++; // sum=sum + *ptr++
 }
 
 // Displaying calculated sum
 printf("Sum of all elements : %d ",sum);
 
 getch(); // Linux user - Remove this

 return 0;
}


COMMON PROGRAMMING ERRORS - POINTERS

COMMON PROGRAMMING ERRORS - POINTERS

COMMON PROGRAMMING ERRORS - POINTERS
  • The asterisk(*) notation used to declare pointer variables does not distribute to all variable names in a declaration. Each pointer must be declared with the * prefixed to the name. Eg: int *x,*y;
  • Dereferencing a pointer that has not been properly initialized or that has not been assigned to point to a specific location in memory is an error. This could cause a fatal execution time error, or it could accidently modify important data and allow the program to run to completion with incorrect result.
  • Being unaware that a  function is expecting pointers as arguments for pass-by-reference and passing arguments by value. Some compilers take the values assuming they're pointers and dereference the values as pointers. At run time, memory-access violation or segmentation faults are often generated. Other compilers catch the mismatch in types between arguments and parameters and generate error messages.
  • Using pointer arithmetic on a pointer that does not refer to an element in an array.
  • Subtracting or comparing two pointers that do not refer to elements in the same array.
  • Running off either end of an array when using pointer arithmetic.
  • Assigning pointer of one type to a pointer of another type if neither is of type void * is a syntax error.
  • Dereferencing a void * pointer is a syntax error.
  • Attempting to modify an array name with pointer arithmetic is a compilation error. 
More Informative Posts: