Page 109 - 6437
P. 109

Value of var = 3000

                            Value available at *ptr = 3000

                            Value available at **pptr = 3000


                          Passing Pointers to Functions
                          C  programming  allows  passing  a  pointer  to  a  function.  To  do  so,  simply  declare  the
                   function parameter as a pointer type.
                          Following is 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;

                 }




                                                                                                     112
   104   105   106   107   108   109   110   111   112   113   114