Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify a struct in a function and return to main?

Tags:

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; 

                }  

program takes in a struct of time, validates it as a 24hr time and prompts user for an update takes an update in HH:MM:SS i.e. if the time was 23:45:00 and update time entered was 01:30:00 then the original time would become 01:15:00.

I want to modify struct one in function updateTime, doesn't seem to be going through the function in the first place. how can I get this to work? currently it will run through the full program without errors BUT it will not change the time and pass it back to the main function, currently just testing with hours.

like image 421
Thomas Hedley Avatar asked Dec 28 '22 11:12

Thomas Hedley


1 Answers

You pass structures to functions like this, don't do it in any other way:

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;
}

Edit: Btw, data->x is exactly the same thing as (*data).x, it is just a bit easier to read the former.

like image 136
Lundin Avatar answered Jan 12 '23 02:01

Lundin