Showing posts with label pointers. Show all posts
Showing posts with label pointers. Show all posts

Thursday, 26 January 2012

Program Of Swapping Using Pointer In C++

#include<iostream.h>
#include<conio.h>
int swap(int *x,int *y);
main()
{

int a,b;
clrscr();

cout<<"enter the first no=";
cin>>a;
cout<<"enter the second no=";
cin>>b;
swap(&a,&b);
cout<<"\nswaping of a="<<a;
cout<<"\nswaping of b="<<b;
getch();
}
int swap(int *x,int *y)
{
int z;
z=*x;
*x=*y;
*y=z;
cout<<"swaping of x="<<*x;
cout<<"\nswaping of y="<<*y;

getch( );
}




Thursday, 19 January 2012

Program Of Pointer To Pointer In C++


#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int a,*b,**c;
a=10;
b=&a;
c=&b;
cout<<"\n Value of a="<<a;
cout<<"\n Value of b="<<b;
cout<<"\n Value of c="<<c;
cout<<"\n Value of c="<<*c;
cout<<"\n Value of c="<<**c;
getch();
}

------------------------------------------------------------------------------

Since we can have pointers to int, and pointers to char, and pointers to any structures we've defined, and in fact pointers to any type in C, it shouldn't come as too much of a surprise that we can have pointers to other pointers. If we're used to thinking about simple pointers, and to keeping clear in our minds the distinction between the pointer itself and what it points to, we should be able to think about pointers to pointers, too, although we'll now have to distinguish between the pointer, what it points to, and what the pointer that it points to points to. (And, of course, we might also end up with pointers to pointers to pointers, or pointers to pointers to pointers to pointers, although these rapidly become too esoteric to have any practical use.)
The declaration of a pointer-to-pointer looks like
 int **ipp;
where the two asterisks indicate that two levels of pointers are involved.
Starting off with the familiar, uninspiring, kindergarten-style examples, we can demonstrate the use of ipp by declaring some pointers for it to point to and some ints for those pointers to point to:
 int i = 5, j = 6; k = 7;
 int *ip1 = &i, *ip2 = &j;
Now we can set
ipp = &ip1;
and ipp points to ip1 which points to i*ipp is ip1, and **ipp is i, or 5. We can illustrate the situation, with our familiar box-and-arrow notation, like this: 


If we say
*ipp = ip2;
we've changed the pointer pointed to by ipp (that is, ip1) to contain a copy of ip2, so that it (ip1) now points at j

If we say
*ipp = &k;
we've changed the pointer pointed to by ipp (that is, ip1 again) to point to k
What are pointers to pointers good for, in practice? One use is returning pointers from functions, via pointer arguments rather than as the formal return value. To explain this, let's first step back and consider the case of returning a simple type, such as int, from a function via a pointer argument. If we write the function
 f(int *ip)
 {
  *ip = 5;
 }
and then call it like this:
int i;
 f(&i);
then f will ``return'' the value 5 by writing it to the location specified by the pointer passed by the caller; in this case, to the caller's variable i. A function might ``return'' values in this way if it had multiple things to return, since a function can only have one formal return value (that is, it can only return one value via the return statement.) The important thing to notice is that for the function to return a value of type int, it used a parameter of type pointer-to-int.


view source:-  http://www.eskimo.com/~scs/cclass/int/sx8.html

Program Of Array of Pointer In C++


#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int i,a[10],*ptr;
for(i=0;i<10;i++)
{
cout<<"Enter NUmber=";
cin>>a[i];
}

for(i=0;i<10;i++)
{
ptr = &a[i];
cout<<"\n Value of a["<<i<<"]==>"<<a[i]<<" And Address of a["<<i<<"]==>"<<ptr;

}

getch();
}

---------------------------------------------------------------------------



As seen in the last example, sorting an array of strings requires swapping the strings which can require copying a lot of data. For efficiency, it is better to avoid actual swapping of data whenever a data item is large, such as a string or an entire data base record. In addition, arrays may be needed in more than one order; for example, we may need an exam scores array sorted by Id number and by weighted scores; or, we may need strings in both an unsorted form and a sorted form. In either of these cases, we must either keep two copies of the data, each sorted differently, or find a more efficient way to store the data structure. The solution is to use pointers to elements of the array and swap pointers. Consider some examples:
int data1, data2, *ptr1, *ptr2, *save;

     data1 = 100; data2 = 200;
     ptr1 = &data1; ptr2 = &data2;
We could swap the values of the data and store the swapped values in data1 and data2 or we could simply swap the values of the pointers:
save = ptr1;
     ptr1 = ptr2;
     ptr2 = save;
We have not changed the values in data1 and data2; but ptr1 now accesses data2 and ptr2 access data1. We have swapped the pointer values so they point to objects in a different order. We can apply the same idea to strings:
char name1[] = "John";
     char name2[] = "Dave";
     char *p1, *p2, *save;

     p1 = name1;
     p2 = name2;
Pointers p1 and p2 point to strings name1 and name2. We can now swap the pointer values so p1 and p2 point to name2 and name1, respectively.In general, an array of pointers can be used to point to an array of data items with each element of the pointer array pointing to an element of the data array. Data items can be accessed either directly in the data array, or indirectly by dereferencing the elements of the pointer array. The advantage of a pointer array is that the pointers can be reordered in any manner without moving the data items. For example, the pointer array can be reordered so that the successive elements of the pointer array point to data items in sorted order without moving the data items. Reordering pointers is relatively fast compared to reordering large data items such as data records or strings. This approach saves a lot of time, with the additional advantage that the data items remain available in the original order. Let us see how we might implement such a scheme.


Wednesday, 18 January 2012

Program Of Call By Value In C++


#include<iostream.h>
#include<conio.h>
int sum(int x,int y);  // Function Delaration
main()
{
clrscr();
int a,b,c;
cout<<"Enter First NUmber=";
cin>>a;
cout<<"Enter Second NUmber=";
cin>>b;
           c=sum(a,b);                 // Function CAlling
cout<<"\n NAswer="<<c;
getch();
}
        int sum(int x,int y)           // Function Define
{
int  z;
z=x+y;
return z;
}

----------------------------------------------------------------------------


Passing a variable by value makes a copy of the 

variable before passing it onto a function. This means

that if you try to modify the value inside a function, it

 will only have the modified value inside that 

function. One the function returns, the variable you 

passed it will have the same value it had before you 

passed it into the function.

Program Of Call By Refrence In C++


#include<iostream.h>
#include<conio.h>
    int sum(int &x,int &y);  // Function Delaration
main()
{
clrscr();
int a,b,c;
cout<<"Enter First NUmber=";
cin>>a;
cout<<"Enter Second NUmber=";
cin>>b;
c=sum(a,b);                 // Function CAlling
cout<<"\n Address of a"<<&a;
cout<<"\n NAswer="<<c;
getch();
}
int sum(int &x,int &y)    // Function Define
{
int  z;
cout<<"\n Address &x"<<&x;
z= x +  y;
return z;
}

-----------------------------------------------------------------------------------

In call by reference, a function passes a reference as an argument to another function. In this case the called function works on the callers copy of parameters and not on a local copy. Before the discussion of call by reference we must know what is a reference.

Program Of Call By Address In C++


#include<iostream.h>
#include<conio.h>
      int sum(int *x,int *y);  // Function Delaration
main()
{
clrscr();
int a,b,c;
cout<<"Enter First NUmber=";
cin>>a;
cout<<"Enter Second NUmber=";
cin>>b;
c=sum(&a,&b);                 // Function CAlling
cout<<"\n NAswer="<<c;
getch();
}
int sum(int *x,int *y)    // Function Define
{
int  z;
z= *x + * y;
return z;
}

-------------------------------------------------------------------------------


In call by address, instead of passing the actual values of the actual argument we pass addresses of actual values. Whenever we deal with addresses, we must know how to handle them. That’s why before discussing call by address, we will briefly discuss pointers that handle addresses.

Sunday, 15 January 2012

Program To Find Divison Of Two Numbers In C++

#include<iostream.h>
#include<conio.h>
 main()
 {
 clrscr();
 int a,b,c;
 cout<<"Enter first number=";
 cin>>a;
 cout<<"Enter second number=";
 cin>>b;
 cout<<c;
 getch();
 }

Saturday, 14 January 2012

Program To Find Sum of Numbers Using Function In C++

#include<iostream.h>
#include<conio.h>
#include<math.h>
main()
{
clrscr();
int a,b,c;
cout<<"Enter numbers=";
cin>>a>>b;
sum(a,b);
getch();
}
 void sum(int x,int y)
 {
 int z;
 z=x+y;
 cout<<z;
 }