Passing pointer to a function in C

By: Emiley J Emailed: 1786 times Printed: 2623 times    

This sample program in C demonstrates the concept of passing a pointer as a parameter to a function. In this program, the function "prompt" accepts two pointers. The second parameter (num) is a pointer to the variable i. Since it is a pointer, the function does not return the value. The value is stored in i itself.

#include 

void prompt(char *msg, int *num);

int main(void)
{
  int i;
  prompt("Enter a num: ", &i);
  printf("Your number is: %d", i);

  return 0;
}

void prompt(char *msg, int *num)
{
  printf(msg);
  scanf("%d", num);
}

Most Viewed Articles (in C )

Latest Articles (in C)

Comment on this tutorial