0

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;


 }
10
  • So what's not working can you tell? Commented Mar 24, 2014 at 6:03
  • Use a debugger, and end all printf format strings with \n or else call fflush Commented Mar 24, 2014 at 6:04
  • Creating pointers to variables just for the sake of maintaining aliases to them which are used whenever the address is required is a poor practice. You can eliminate the h, m and s variables, and just call time as time(&hours, &minutes, &seconds, num). That function has to take pointers; there are no reference parameters in C. Commented Mar 24, 2014 at 6:30
  • There is already a time function in the C language, so when you define your own external function called time, you're invoking undefined behavior. Commented Mar 24, 2014 at 6:31
  • @Kaz; How could you say that it invokes UB? Commented Mar 24, 2014 at 6:36

2 Answers 2

3

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Ah how did i miss this!!
0

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.

1 Comment

I've added the explanation for the program, Thanks for your TIP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.