Demonstrates passing a variable to a function by address. - C Function

C examples for Function:Function Parameter

Description

Demonstrates passing a variable to a function by address.

Demo Code

#include <stdio.h>

void half(int *i) // Receives the address of i
{
   *i = *i / 2;/*www. j  a  v  a 2  s  . co  m*/

   printf("Your value halved is %d.\n", *i);

   return; // Returns to main

}
int main()
{
   int i;

   printf("Please enter an integer... ");
   scanf(" %d", &i);

   // Now call the half function, passing the address of i

   half(&i);

   // Shows that the function did alter i's value

   printf("In main(), i is now %d.\n", i);

   return(0); // Ends the program
}

Related Tutorials