编写一个程序,接受用户的总秒数。将此值与三个变量(小时、分钟、秒)的地址一起传递给一个名为time()的函数,该函数将计算小时、分钟和秒数。从main()打印此信息。
请帮助我plz如何修复我的代码,使这个程序按照它应该的方式工作。

   /* 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;


 }

最佳答案

C中没有按引用调用,只有按引用传递。
变化

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


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

time的函数定义中,更改
 hr = &h;
 min = &m;
 sec = &s;


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

关于c - 具有函数的c指针,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22602115/

10-11 23:02
查看更多