Programming Tutorials

Passing pointer to a function in C

By: Emiley J in C Tutorials on 2010-04-16  

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 <stdio.h>

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);
}






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in C )

Latest Articles (in C)