Basics of Pointers :-
Definition :-
The pointer is nothing but an address. The pointers are useful for indirect access. The pointers are useful for the implementation of call by address.
Declaration of pointer variable –
The general syntax of declaring pointer variable is as follows:-
datatype * pointername
for e.g.
int * p ;
float * q ;
char * r ;
In the above declaration the p will act as pointer to integer, the ‘q’ will act as pointer to float and the ‘r’ will act as pointer to char. In other words the p will store address of integer the ‘q’ will store address of float and the ‘r’ will store address of char.
Referencing and De-referencing :-
The address of operator (&) is used for referencing and the indirection operator (*) is used for. De-referencing. To understand the concept of referencing and de-referencing let us consider the following example.
# include
void main ( )
{
int x = 7 ;
int * p ;
p = & x
cout << * p << “ is stored at address ” << p << ‘ \n ’ ;
}
1) In the above pgm the p is declared as pointer to integer.
2) Consider the statement, p = & x ; In this statement. We are using address of operator i.e & due to this statement the address of x is copied in to p. In other words the p will store address of x i.e. p will act as pointer to x i.e. p will act as reference to x. This process is called as referencing. This is because it is possible to access x with its reference i.e. address i.e. pointer i.e. p.
3) In the cout statement we are printing the value of * p. The operator * is nothing but indirection operator. The indirection operator operates on address i.e. reference and gives value stored at that address. Since p stores the address of x, the value of * p is nothing but the value of x. thus we can refer x indirectly as * p and hence the indirection operator. We can also say that by applying the indirection operator on the pointer p i.e. reference p we are getting the original value of x hence this
concept is called as de-referencing. Hence the operator * is called as de-referencing operator.
Write the output of the following program:-
# include
void main ( )
{
Int x = 7, y ;
int * p, * q ;
P = & x ;
q = & y;
* q = * p + 100 :
cout ‘\t’ ‘<< * p << ‘\n’ ;
cout << y << ‘\t’ << * q << ‘\n’ ;
Pointer to pointer :-
The general syntax of declaring pointer is as follows.
dataype * * pointer name
The e.g. will be as follows
int ** p ;
In the above example the p is pointer to pointer integer. In other words the p stores address of address of integer.
Write the output of the following pgm
# include
void main ( )
{
int x = 7 ;
int * p ;
int * * q ;
p = & x ;
q = & p ;
cout << x << “ is stored at ” << p << ‘ \n ’ ;
cout << * p << “ is stored at ” << p << ‘ \n ’ ;
cout << * * q << “ is stored at ” << * q << ‘ \n ’ ;
cout << * q << “ is stored at ” << q << ‘ \n ’ ;
}
Call by value / Pass by value / Parameter passing by value –
# include
void main ( )
{
int x = 7, y = 5 ;
void swap ( int, int ) ;
cout << “Before exchange = ” ;
cout << “ x = ” << x << “ , ” << “ y = ” << y << ‘ \n ’ ;
swap ( x, y ) ;
cout << “After exchange = ” ;
cout << “ x = ” << x << “ , ” << “ y ” << y << ‘\n’ ;
}
void swap( int p, int q)
{
int temp ;
temp = p ;
p = q ;
q = temp ;
}
Output :-
Before exchange
x = 7, y = 5
After exchange
x = 7, y = 5
When the swap ( ) is called, then the values of actual parameters x & y are passed and copied into formal parameters i.e. p and q. Hence the concept is referred as call by value or pass by value. The actual parameters and formal parameters occupy separate memory locations and hence exchange taking place in p and q will not cause changes in x and y. Thus call by value is useful for one-way communication through parameters.
Call by Address / call by pointer / call by reference:-
# include
void main ( )
{
int x = 7, y = 5 ;
void swap ( int *, int *)
cout << “ Before exchange = ” ;
cout << “ x = ” << x << “ , ” << “ y = ” << y << ‘ \n ’ ;
swap (&x, &y) ;
cout << “After exchange = ” ;
cout << “ x ” < } Void swap ( int * p , int * q ) { int temp ; temp = * p ; * p = * q; * q = temp; } Output: - Before exchange x = 7, y = 5 After exchange x = 5, y = 7 1) When the swap ( ) is called. Then the address of x & y i.e. in other words reference of x & y are passed as parameters. Hence the concept is called as call by reference or call by address or pass by reference or pass by address. When the swap ( ) is called, the address of x and y are copied in to pointer variable named as p and q. The logic within the swap ( ) exchanges the value of variables which are being pointed by p and q. Thus with the help of reference p and q and by de-referencing them, the values of x and y itself are exchanged. Thus call be address i.e. call by reference is useful for two way communication. Call by Alias // Call by Reference // Pass by Reference / Pass by Alias :- # include void main ( ) { int x =7, y=5 ; void swap( int & , int &) ; cout << “ Before exchange = ” ; cout << “ x = ” << x << “ y = ” << y << ‘ \n ’ ; swap ( x, y) ; cout << “After exchange = ” ; cout << x = “ << x << ” , “ , ” << “ y = ” << y << ‘ \n ’ ; } void swap ( int & p , int & q ) { int temp ; temp = p ; p = q ; q = temp ; } O/P :- Before exchange x = 7 , y = 5 After exchange x = 5 , y = 7 When the swap ( ) is called then x & y are passed as parameters and the formal parameters p and q will act as their alias. Hence the concept is called as call by alias or pass by alias. The p and q will act as other names of x and y. in other words we can say that p and q acts as references of x and y. hence the concept is also called as call by reference or pass by reference. One dimensional Arrays and Pointers : - One dimensional array name is written without subscript is nothing but pointer to the 0th element of the array or I other words address of the 0thelement of the array. Consider the following example: int a [ 50 ] ; Note 1 :- a ↔ & a [ 0 ] a + 0 ↔ & a [ 0 ] a + 1 ↔ & a [ 1 ] a + 2 ↔ & a [ 2 ] . . . . . . . a + i ↔ & a [ i ] Note 2 :- * ( a + i ) ↔ a [ i ] Write the out put of the following pgm : # include void main ( ) { int a [5] = {10, 20, 30, 40, 50} ; int i ,* y ; y = 0; for ( i =0 , i<5 ; i + + ) cout << y [ i ] << ‘ \t ’ ; } Out put : 10 20 30 40 50 In the above pgm, y acts as another name of array a. Write the out put of the following pgm :- # include void main ( ) { int a [ 5 ] = { 10, 20, 30, 40, 50 } ; int i ; for ( i = 0 , i < 5 ; i + + ) cout << i [ a ] << ‘ \n ’ ; } Out put : 10 20 30 40 50 * Example of two dimensional array as parameter of function. 1) Write a menu driven pgm that will provide options for addition subtraction. Multiplication of matrices and transpose of a matrix. The pgm should give facility for repeated execution till user enters the option quit. WAP with the help of modular programming approach. # include # include void main ( ) { int a [ 10 ] [ 10 ] , b [ 10 ] [10] , c [ 10 ] [ 10 ] ; int m1, n1, m2, n2, option ; void readmat ( int [ ] [ 10 ] , int , int ) void printmat ( int [ ] [ 10 ] , int , int ) void multmat ( int [ ] [ 10 ] , int [ ] [ 10 ] , int + [ ] [ 10 ] , int , int , int ) void transpose ( int [ ] [ 10 ] , int [ ] [ 10 ] int , int ) void addmat ( int [ ] [ 10 ] , int [ ] [ 10 ] , int [ ] [ 10 ] , int , int ) void subtract ( int [ ] [ 10 ] , int [ ] [ 10 ] , int [ ] [ 10 ] , int , int ) ; do { clrscr ( ) ; cout << “1 : addition\n” ; cout << “2 : subtraction\n” ; cout << “3 : multiplication\n” ; cout << “4 : transpose\n” ; cout << “5 : quit \n” ; cout << “Enter your option =” ; cin >> option ; if ( option > = 1 && option < = 3 ) { cout << “Enter order of first matrix =” ; cin >> m1 >> n1; cout << “Enter order of second matrix =” ; cin >> m2 >> n2; cout << “Enter element of first matrix =” ; readmat ( a , m1 , n1 ) ; cout << “Enter element of second matrix =” ; readmat ( b , m2 , n2 ) ; switch ( option ) { case 1 : if ( m1 = = m2 && n1 = = n2 ) { addmat ( a , b , c , m1 , n1 ) ; printmat ( c , m1 , n1 ) ; } else cout << “ order mismatch \n ” ; break ; case 2 : if ( m1 = = m2 && n1 = = n2 ) { submat ( a , b , c , m1 , n1 ) ; printmat ( c , m1 , n1 ) ; } else cout << “ order mismatch \n ” ; break ; case 3 : if ( m1 = = m2 ) { multmat ( a , b , c , m1, n2, n1 ) ; printmat ( c , m1, n2 ) ; } else cout << “ order mismatch \n ” ; break ; } else if ( option = = 4 ) { cout << “ Enter order of matrix = ” ; cin >> m1 >> n1 ; cout << “ Enter element = ” ; readmat ( a , m1 , n1 ) ; transpose ( a , b , m1 , n1 ) ; printmat ( b , m , m1 ) ; } getch ( ) ; } //closing curly brackets of do while ( option ! = 5 ) ; } void readmat ( int p[ ] [ 10 ] , int x , int y ) { int i , j ; for ( i = 0 ; i < x ; i + + ) for ( j = 0 ; j < y ; j + + ) cin >> p[ i ] [ j ] ; } void printmat ( int p[ ] [ 10 ] , int x , int y ) { int i , j ; for ( i = 0 ; i < x ; i + + ) for ( j = 0 ; j < y ; j + + ) cout << p [ i ] [ i ] << ‘ \t ’ : cout << ‘ \n ’ ; } } void transpose ( int p[ ] [ 10 ] , int q[ ] [ 10 ] , int x , int y ) { int i , j ; for ( i = 0 ; i < x ; i + + ) for ( j = 0 ; j < y ; j + + ) q[ j ] [ i ] = p[ i ] [ j ] ; } void multmat ( int p[ ] [ 10 ] , int q[ ] [ 10 ] , int r [ ] [ 10 ] , int x , int y , int = ) { int i , j , k ; for ( i = 0 ; i < x ; i + + ) for ( j = 0 ; j < x ; j + + ) r[ i ] [ j ] = 0 ; for ( k = 0 ; k < z ; k + + ) r[ i ] [ j ] = r[ i ] + [ j ] + p[ i ] [ k ] * q [ k ] [ j ] ; } void addmat ( int p[ ] [ 10 ] , int q[ ] [ 10 ] , int r [ ] [ 10 ] , int x , int y ) { int i , j ; for ( i = 0 ; i < x ; i + + ) for ( j = 0 ; j < y ; j + + ) r[ i ] [ j ] = p[ i ] [ j ] + q[ i ] [ j ] ; } void submit (int p[ ] [ 10 ] , int q[ ] [ 10 ] , int r [ ] [ 10 ] , int x , int y ) { int i , j ; for ( i = 0 ; i < x ; i + + ) for ( j = 0 ; j < y ; j + + ) r[ i ] [ j ] = p[ i ] [ j ] + q[ i ] [ j ] ; } } ( A x B )T = BT x AT The pgm should have following functions. → # include # include void main ( ) { int a [ 10 ] [ 10 ] , b [ 10 ] [10] , c [ 10 ] [ 10 ] ct [ 10 ] [ 10 ], bt [ 10 ] [ 10 ] , at [ 10 ] [10] , mt [ 10 ] [ 10 ] ; int m1, n1, m2, n2 ; void readmat ( int [ ] [ 10 ] , int , int ) ; void printmat ( int [ ] [ 10 ] , int , int ) ; void transpose ( int [ ] [ 10 ] : int [ ] [ 10 ] int , int ) void multmat ( int [ ] [ 10 ] , int [ ] [ 10 ] , int [ ] [ 10 ] int , int ) int equal ( int [ ] [ 10 ] , int [ ] [ 10 ] , int , int ) clrscr ( ) ; cout << “Enter order of first matrix =” ; cin >> m1 >> n1 ; cout << “Enter order of second matrix =” ; cin >> m2 >> n2 ; if ( n1 = = m2 ) { cout << “Enter element of first mat =” ; readmat ( a, m1, n1 ) cout << “Enter element of second mat =” ; readmat ( b, m2, n2 ) multmat ( a, b, c, m1, n2, n1 ) ; transpose ( c, ct, m1, n2 ) ; transpose ( b, bt, m2, n2 ) ; transpose ( a, at, m1, n1 ) ; multmat ( bt, at, mt, n2, m1, n1 ) ; cout << “ L.H.S. \n ” ; printmat ( ct, n2, m1 ) ; cout << “ R.H.S. \n ” ; printmat ( mt, n2, m1 ) ; if equal ( ct, mt, n2, m1 ) cout << “ property proved \n ” ; } else cout << “ Order mismatch \n ’ : getch ( ) ; } void readmat( int p[ ] [ 10 ] , int x , int y ) { int i, j ; for ( i = 0 ; i < x ; i + + ) for ( j = 0 ; j < y ; j + + ) cin >> p [ i ] [ j ] ; } void printmat ( int p[ ] [10] , int x , int y ) { int i, j ; for ( i = 0 ; i < x ; i + + ) { for ( j =0 ; j < y ; j + + ) cout << p [ i ] [ j ] << ‘ \t ’ ; cout << ‘ \n ’ ; } } void transpose ( int p[ ] [10] , int q[ ] [10] , int x, int y ) { int i, j ; for ( i = 0 ; i < x ; i + + ) for ( j = 0 ; j < y ; j + + ) q [ j ] [ i ] < p[ i ] [ j ] ; } void multmat ( int p[ ] [ 10 ] , int q[ ] [ 10 ] , int r[ ] [ 10 ] , int x , int y , int z ) ; } int i, j , k ; for ( i = 0 ; i < x ; i + + ) for ( j = 0 ; j < y ; j + + ) { r [ i ] [ j ] = 0 ; for ( k = 0 ; k < z ; k + + ) r [ i ] [ j ] = r [ i ] [ j ] + p [ i ] [ k ] * q [ k ] [ j ] ; { int equal ( int p[ ] [ 10 ] , int q[ ] [ 10 ] , int x , int y ; { int i, j, count = 0 ; for ( i = 0 ; i < x ; i + + ) for ( j = 0 ; j < y ; j + + ) if ( p [ i ] [ j ] = = q[ i ] [ j ] ) count + + ; if ( count = = x * y ) return 1 ; else return 0 ; } Two Dimensional Array And Pointers Two dimensional array name written with out subscript is nothing but the pointer to pointer to the element in the 0th row and 0th column. Consider the following example. int a [ 3 ] [ 4 ] ; * Example of one dimensional array as parameter to function : IMP : - a [ 0 ] → 0th element of an array a. a → address of pointer to the 0th element of an array a. a) void main ( ) { int a[ 100 ] ; int i , n, x, ans ; int search ( int [ ] , int , int ) ; clrscr ( ) ; cout << “ How many elements = ” ; cin >> n ; cout << “ Enter elements = ” ; for ( i = 0 ; i < n ; i + + ) cin >> a [ i ] ; cout << “Enter the element to be searched” ; cin >> x ; ans = search ( a, n, x ) ; if ( ans = = 0 ) ; cout “ is not present ” ; else cout “ is present ” << ans << “times \n ” ; getch ( ) ; } int search ( int p[ ] , int n, int x ) { int i , count = 0 ; for ( i = 0 ; i < n ; i + + ) if ( p [ i ] = = x ) count + + ; return count ; } b) void main ( ) { int a [100] ; int i, n, x, ans ; int search (int *, int, int) ; clrscr ( ) ; cout << “How many element” ; cin > n ; cout << “Enter element =” ; for ( i = o; i < n; i + +) cin >> *( a + i) ; cout << “Enter element to be searched =” ; cin >> x ; ans = search (a, n, x) ; if (ans = = 0) cout << x << “is not present in” ; else cout << x << “is present << count <<” times \n” ; getch ( ) ; } int search (int * p, int n, int x) { int i, count = 0 ; For ( i =0; i < n; i + +) If (* ( p + i) = = x) count + + ; return count ; } 2) Write a function that accepts that accepts one dimensional array of not more than 100 integers and it should also accept the element to be searched. The function should return the position of the first occurrence of that element in the array. If the element is not present then it should return –1. Write a suitable main function that will search the element in the array using the above function use pointer notation. # include # include void main ( ) { int a [100] ; int i, n, x, ans ; int search ( int *, int , int ) clrscr ( ) ; cout << “Enter the element = ” ; for ( i = 0 ; i < n ; i + + ) cin >> * (a + i) ; cout << “Enter element to be searched =” ; cin >> x ; ans = search ( a, n, x) ; if ( and = = –1) cout << x << “is not present \n” ; else cout << x << “is present at position” << ans << ‘\n’ ; getch ( ) ; } int search ( int * p, int n, int x ) { int i = 0 ; while ( i < n & & * (p + i) ! = x) i + + ; if ( i < n ) return 1 ; else return – 1 ; } 3) Write a function that accepts a string and returns the answer indicating whether that string is palindrome or not write a suitable main function. # include # include # include # include void main ( ) { char str [80] ; int ans ; int palindrome ( char [ ] ) ; clrscr ( ) ; cout << “Enter a string =” ; gets (str) ; ans = palindrome (str) ; if ( ans = = 1) cout << “It is palindrome \n” ; else cout << “It is palindrome \n” ; getch ( ) ; } int palindrome ( char [ ]) { int i, n; flag = 1 , n = strlen (s) ; for ( i = 0 ; i < n/2 & & flag ; i + + ) if ( s [ i ] ! = s [ n – 1 – i ] ) flag = 0 ; return flag ; } ARRAY OF POINTERS :- The general syntax of declaring one dimensional array of pointers is as follows. datatype * arrayname [ size ] For e.g. int * a [ 5 ] ; In the above example, a is an array of 5 pointers and all the pointers are pointer to integer. a [ 10 ] a [ 1 ] a [ 2 ] a [ 3 ] a [ 4 ] The general syntax of declaring two dimensional array of pointers is as follows. datatype * arrayname [rowsize] [columsize] for e.g. float * b [ 3 ] [ 4 ] In above example, b is an array of 3 rows and 4 columns and all the 12 elements are pointers and those are pointers to floating point data.