user3449152
user3449152

Reputation: 5

c pointers with functions

Write a program that will accept from the user the total number of seconds. Pass this value along with the addresses of three variables – hours, minutes, seconds – to a function called time() that will calculate the number of hours, minutes and seconds. Print this information from main().

Help me plz how can i fix my code to make this program work the way it supposed to.

   /* Adham Hamade
 .
  */

#include <stdio.h>
#include <conio.h>

//function prototype
void time(int &,int &,int &, int);


    int main()
   {
   //Variables
    int num;
    int hours;
    int minutes;
    int seconds;


    //reference number variables 
    int *h = &hours;
    int *m = &minutes; 
    int *s = &seconds;


    printf("Please enter number of seconds");
    scanf("%d",&num);

    time(h, m, s, num);

    printf("\n\nTime is %d hrs %d mins %d secs", hours, minutes, seconds);


 getch();
 return 0 ;   
 }

 void time(int &h,int &m ,int &s, int num)
 {
      int sec;
      int min;
      int hr;
      int t;


      hr = num / 3600 ;
      t = num %3600;
      min = t/60;
      sec = t%60;

      hr = &h;
      min = &m;
      sec = &s;


 }

Upvotes: 0

Views: 121

Answers (2)

haccks
haccks

Reputation: 105992

There is no call by reference in C, only pass by reference.
Change

void time(int &,int &,int &, int);  

to

void time(int *, int *, int *, int);  

In your function definition of time, change

 hr = &h;
 min = &m;
 sec = &s;

to

*h = hr;
*m = min;
*s = sec;

Upvotes: 3

Sorcrer
Sorcrer

Reputation: 1644

Try this , just modified your program

#include <stdio.h>

//function prototype
 void time(int *h,int *m ,int *s, int num);


int main()
{
   //Variables
    int num;
    int hours;
    int minutes;
    int seconds;

    //reference number variables 
    int h ;
    int m ; 
    int s;


    printf("Please enter number of seconds");
    scanf("%d",&num);

    time(&hours, &minutes, &seconds, num);

    printf("\n\nTime is %d hrs %d mins %d secs", hours, minutes, seconds);


 getch();
 return 0 ;   
 }

 void time(int *h,int *m ,int *s, int num)
 {
      int sec;
      int min;
      int hr;
      int t;

      hr = num / 3600 ;
      t = num %3600;
      min = t/60;
      sec = t%60;

      *h= hr ;
      *m = min ;
      *s =sec ;
 }

The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.

For passing functions by reference, just pass the address of the variable which is to be modified ie; address of hours, minutes, seconds in the function call. The address is obtained by prepending ampersand operator '&' . So giving '&hours' gives the address of the variable hours which is used in the function call gets assigned to a pointer in the function definition.

void time(int *h,int *m ,int *s, int num);

so a function call like

time(&hours, &minutes, &seconds, num);

defines that pointer *h points to the address of the variable hours,

pointer *m points to the address of the variable minutes,

pointer *s points to the address of the variable seconds,

and the value of number of seconds is assigned to num

So any value assigned to the pointers reflects in the main function since they are referring to the memory location.

Upvotes: 0

Related Questions