Computer Concept & Programming In C - Unit IV - 10

Q.19. What do you understand by pointer to pointer? Also discuss its advantages and disadvantages. 
Ans. A pointer to a pointer is a variable which holds the address of another pointer variable.
Pointer to a pointer makes a program more complexes to understand which is a disadvantage.
It gives a flexiblity to the user to access more than one data structure by a single pointer to a pointer variable which is its advantage.

Q.20. What do you understand by pointer to pointer? Also discuss its advantages and disadvantages. 
Ans. A pointer to a pointer is a variable which holds the address of another pointer variable.
Pointer to a pointer makes a program more complexes to understand which is a disadvantage.
It gives a flexiblity to the user to access more than one data structure by a single pointer to a pointer variable which is its advantage.

Q.21 Differentiate between call by value and call by reference. Make a program in C to show the usage of both.                                            (AKTU. 2009-10)
Related Questions -
Q. What do you mean by Call by Value technique for function call? Explain with suitable example.                                                                   (AKTU. 2010-11)
Q. What are the different ways of passing paramaters to the function? Explain with example.                                                                          (AKTU. 2011 - 12, 13 - 14)
Ans.
The call by value and call by value are also known as pass by value and pass by reference respectively. 
Differences between pass by reference and pass by value
1)Pass by value always invokes / calls the function or returns a value that is based on the value. This value is passed as a constant or a variable with value.
2)Pass by reference always invokes / calls the function by passing the address or a pointer to a memory location which contains the value. The memory location / pointer is populated with a value, so the function could look at the value through that location. The function can update the value available in the memory location by referencing the pointer. A string in C language is passed by reference.  

  C program of call by Value
  
  /*A C Program to find whether
   a given number is prime or not */

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define true 1
#define false 0
int isprime(long);
void main()
{
 long num;
 int result;
 clrscr();

 printf("\nEnter a positive integer:\n");
 scanf("%ld",&num);

 result = isprime(num);

 if(result == true )
    printf("\n%d  is a Prime Number", num);
 else
    printf("\n%d is not a Prime Number", num);

 getch();

}

int isprime(long n)
{
 long i, sq_rt;
 sq_rt = sqrt(n);
 for( i = 2; i < sq_rt + 1; i++)
    if(n % i == 0)
       return false;
 return true;
}

 In the preceding program isprime() function is called by value.

 C program of call by reference

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define true 1
#define false 0
int isprime(long*);
void main()
{
 long *num;
 int result;
 clrscr();

 printf("\nEnter a positive integer:\n");
 scanf("%ld",num);

 result = isprime(num);

 if(result == true )
    printf("\n%d  is a Prime Number",*num);
 else
    printf("\n%d is not a Prime Number",*num);

 getch();

}

int isprime(long *n)
{
 long i, sq_rt;
 sq_rt = sqrt(*n);
 for( i = 2; i < sq_rt + 1; i++)
    if((*n) % i == 0)
       return false;
 return true;
}

In the preceding program isprime() function is called by reference.
There are two ways of passing parameters to a function i.e.
Call by value and
Call by reference