Tuesday 17 September 2013

pointer

Q.Write a "C" Program using function to swap to number

#include<stdio.h>
#include<conio.h>
/* function prototype */

void swap(int* ,int*);

void main()
{
     int a,b;

     clrscr();

       printf("\nEnter the value of a and b:");
     scanf("%d %d",&a,&b);

     printf("\nBefore Swap , a = %d , b=%d",a,b);

     swap(&a,&b); /*function call */

     printf("\nAfter Swap , a = %d , b=%d",a,b);

     getch();
}

/*function definition */

void swap(int *a,int *b)
{
     int c;

     c=*a;
     *a=*b;
     *b=c;


}
    
Find the output of the following program :

(i)
     #include<stdio.h>    
     #include<conio.h>
     main()
     {
          int i=4,j=5;

          junk(i,&j);

          printf("\n %d %d",i,j);
     }
     junk(int i,int *j)
     {
          i=i*i;

          *j=*j**j;
     }


i=4
j=25




             
#include<stdio.h>
#include<conio.h>
void main()
{
     int i=16;

     float f=3.14;

     char c='A';

     char *cc;

     cc=&c;

     printf("\n cc = %u  , *cc= %c",cc,*cc);

     cc=&i;

     printf("\n cc = %u  , *cc= %c",cc,*cc);

     cc=&f;

     printf("\n cc = %u  , *cc= %c",cc,*cc);

     getch();
}


####################################################################






Reference Variables :
The reference variable will store the reference of the variable. The general form of declaring the reference variable is ,
     datatype & referencevariablename=variablename;
e.g.
     int a=10;
     int &b=a;
     Now, b will be the reference variable and it will reference to a.
The reference variable is prefixed by an ampersand sign at the time of its declaration.     As , the pointer variable will store the address of the variable , the reference variable will store    the     refernce of the variable. That is , it will reference to the same location to which the variable    is referencing to.
     In case of pointers ,
          int a=10;
          int *b;
          b=&a;
                   

     In case of reference variable,
          int a=10;
          int &b=a;
              

          So , if we make any change in b , the change will get reflected in a and vice versa.
In c++, the reference variable can be used in three different ways ,

     (a) Independent Reference variable
     (b) Passing references to the functions
     (c) Returning references from the function.

     (a) Independent References Variables :
          Consider the following programs ,
     #include<iostream.h>
     #include<conio.h>
     void main()
     {
          int a=10;
          int &b=a;
cout<<endl<<"a= " << a <<" b="<<b;
          a=a+2;
          cout<<endl<<"a= " << a <<" b="<<b;
          b=b-1;
cout<<endl<<"a= " << a <<" b="<<b;
          int c=50;
          b=c;

cout<<endl<<"a= " << a <<" b="<<b<<"c="<<c;
          b=b+5;
          cout<<endl<<"a= " << a <<" b="<<b<<"c="<<c;
          getch();
}
output :
a=10 b=10
a=12 b=12
a=11 b=11
a=50 b=50 c=50
a=55 b=55 c=50
(b) Passing references to the function
Consider the following program ,

#include<iostream.h>
#include<conio.h>
/*function prototype*/
void swap(int & , int &);
void main()
{
     int a,b;

     clrscr();
cout<<endl<<"Enter the value of a and b:";
     cin>>a>>b;
cout<<endl<<"Before Swap a="<<a<<" b= "<<b;
swap(a,b); /*function call*/
cout<<endl<<"After Swap a="<<a<<" b="<<b;
getch();
}
/*function definition*/
void swap(int & x , int &y)
{
     int z;

     z=x;

     x=y;

     y=z;
}

Now , we have used the reference variables in the formal arguments ,so they will reference to the actual arguments , so the changes which we make in the formal arguments will also get reflected in the actual arguments.This is known as Call By Reference

(c) Returning References from the functions .

     Consider the following program ,

     #include<iosteam.h>
     #include<conio.h>
     /*fucntion prototypr*/

     char & func(int);

     /*global declaration */

          char s[ ] = "Hello World";

     void main()
     {
          func(5)='*'; /*function call*/

    
          cout<<endl<<s;
    
          getch();
     }

     /*function defintion */

     char & func(int pos)
     {

          return s[pos];
     }


output : Hello*World

No comments:

Post a Comment