Wednesday

Passing pointers to functions in C


Passing pointers to functions in C

C programming language allows you to pass a pointer to a function. To do so, simply declare the function parameter as a pointer type.
Following a simple example where we pass an unsigned long pointer to a function and change the value inside the function which reflects back in the calling function:

#include <stdio.h>
#include <time.h>

void getSeconds(unsigned long *par);

int main ()
{
   unsigned long sec;


   getSeconds( &sec );

   /* print the actual value */
   printf("Number of seconds: %ld\n", sec );

   return 0;
}

void getSeconds(unsigned long *par)
{
   /* get the current number of seconds */
   *par = time( NULL );
   return;
}
When the above code is compiled and executed, it produces following result:
Number of seconds :1294450468

Kindly Bookmark this Post using your favorite Bookmarking service:
Technorati Digg This Stumble Stumble Facebook Twitter
YOUR ADSENSE CODE GOES HERE

0 comments:

Post a Comment

Dont Forget for Commets

 

| C programing tutorials © 2009. All Rights Reserved | Template Style by My Blogger Tricks .com | Design by Brian Gardner | Back To Top |