Google News
logo
Pointer and functions In C Language
• Pointers can be passed as argument to a function definition.
• Passing pointers to a function is called call by reference.
• In call by reference whatever changes are made in formal arguments of the function definition will affect the actual arguments in calling function. 
• In call by reference the actual arguments must be pointers or references or addresses.
• Pointer arguments are use full in functions, because they allow accessing the original data in the calling program.
  Program : The following program is an example of pointer and functions
# include < stdio.h>
void swap (int *, int *);

main ( ) 
{
   int a=10, b=20; 
  swap(&a, &b); 	/* a,b are actual parameters */
  printf (“a= %d \t b=%d ”, a ,b); 
 }
      void swap (int *x , int *y)   /* x, y are formal parameters */ 
  {
   int t;
   t = *x;
   *x=*y;
   *y=t;
  }
Output :

a=20 b= 10

  Program : The following program is an example of pointer to functions.
# include < stdio.h>
# include < conio.h>
 void copy (char *, char *);
 main ()
   {
  char a[20], b[20]; 
  printf (“ Enter string a:”);
  gets(a);
  printf (“ Enter string b:”);
  gets(b);
  printf (“ Before copy “ );

  printf (“ a=%s  and b=%s ”, a , b);
  copy(a ,b);
  printf (“After copy “);
  printf (“ a=%s and b=%s”, a , b); 
   }
  void copy (char *x, char *y)
  { 
  int i=0;
  while ((X[i]=Y[i]) != “\0‟) 
    i++;
}
Output :

Enter string a: computer
Enter string b: science
Before copy
a=computer and b= science
After copy
a=science and b=science