We have two programs. First program is
whit POINTER and the second program is without the Pointer.
Both program print out the same result. but why we should use POINTER.
Code:
/*Cube a variable using call-by-reference with a pointeargument*/
#include <stdio.h>
void cubeByReference( int *nPtr );
int main()
{
int number = 5;
printf( "The original value of number is %d", number );
cubeByReference( &number );
printf( "\nThe new value of number is %d\n", number );
return 0;
}
void cubeByReference( int *nPtr )
{
*nPtr = *nPtr * *nPtr * *nPtr;
}
Code:
#include <stdio.h>
int cubeByValue( int n ); /* prototype */
int main()
{
int number = 5;
printf( "The original value of number is %d", number );
number = cubeByValue( number );
printf( "\nThe new value of number is %d\n", number );
return 0;
}
int cubeByValue( int n )
{
return n * n * n;
}