如何修改函数中的结构并返回main?

时间:2011-11-11 14:05:34

标签: c

#include <stdio.h>
#include <stdlib.h>


struct time{int hours, mins, secs;};

int main(int argc, char *argv[])
{
struct time one;
struct time two; 

printf("\nplease enter the time in 24 hr format, \nenter the hours, return,\nenter minutes, return, enter seconds, and return.\n");

scanf("%d\n%d\n%d", &one.hours, &one.mins, &one.secs); 
int yn; 

yn = validateTime(one, yn); 
while(!yn){ 


      if (!yn){
         puts("Invalid input\nPlease try again"); 
         printf("\nplease enter the time in 24 hr format, \nenter the hours, return,\nenter minutes, return, enter seconds, and return.\n");
         scanf("%d\n%d\n%d", &one.hours, &one.mins, &one.secs);  
         yn = validateTime(one);
      }
      else{ 
         printf ("Time entered was; %d:%d:%d", one.hours, one.mins, one.secs);  
      }
      }
         printf ("the time entered ws; %d:%d:%d", one.hours, one.mins, one.secs);
char input[4]; 
puts("would you like to update the time"); 
scanf("%s", input); 
if(strcmp(input,"yes")== 0){ 
      puts("please enter update for time"); 
      scanf("%d\n%d\n%d", &two.hours, &two.mins, &two.secs); 
      if (two.hours > 0|| two.mins > 0 || two.secs > 0){
          struct time updateTime(one, two);   
      printf ("%d", one.hours);
      }
      }
  getch();
  return 0;  
} 


int validateTime(struct time tme, int yn)
{

if (tme.hours < 0 || tme.hours > 23 || tme.mins > 59 || tme.mins < 0 || tme.secs < 0 || tme.secs > 59)
{
              printf("retfal4");
     yn = 0;
     return yn;
     }
else {
     printf("rettru");
     yn = 1;
     return yn; 

     }
}
struct time updateTime(struct time one, struct time two){
       puts("flag1");

        struct one;
        struct two;  

                 puts("flag");
                one.hours = one.hours + two.hours;

                one.mins = one.mins + two.mins; 


               one.secs = one.secs + two.secs; 

                  while (two.hours > 23) {
                    one.hours = one.hours - 24;}

                    while (two.mins > 59){
                    one.mins = one.mins - 60;
                    one.hours ++;}

                    while (two.secs > 59){
                    one.secs = one.secs - 60;
                    one.mins ++;}  

                return one; 

                }  

程序采用时间结构,将其验证为24小时,并提示用户进行更新,在HH:MM:SS中进行更新,即如果时间是23:45:00,输入的更新时间是01:30 :00然后原来的时间将变成01:15:00。

我想在函数updateTime中修改struct one,似乎没有首先通过该函数。我怎么能让这个工作?目前它将在没有错误的情况下运行整个程序但是它不会改变时间并将其传递回主函数,目前只是用几小时进行测试。

3 个答案:

答案 0 :(得分:6)

您将结构传递给这样的函数,不要以任何其他方式执行:

typedef struct    // use typedef for convenience, no need to type "struct" all over the place
{
  int x;
  int y;
} Data_t;


void function (Data_t* data); // function declaration

int main()
{
  Data_t something = {1, 1};  // declare a struct variable and initialize it
  printf("%d %d\n", something.x, something.y);

  function (&something); // pass address of "something" to the function

  /* since it was passed through a pointer (by reference), 
     the original "something" struct is now modified */

  printf("%d %d\n", something.x, something.y);
}

void function (Data_t* data)  // function definition
{
  data->x = 2; // access a struct member through a pointer using the -> operator
  data->y = 2;
}

编辑:顺便说一下,data->x(*data).x完全相同,只是更容易阅读前者。

答案 1 :(得分:5)

为什么不使用指向结构的指针?您需要阅读并了解有关它们的更多信息。

答案 2 :(得分:0)

您正在将该功能称为:

struct time updateTime(one, two);   

这是不正确的。它实际上是一个函数声明而不是一个调用。