'convert time to seconds struct in c

The above code is to convert a time to seconds in c using struct I dont know how to use stdarg in function please help me...

i created a struct called time.

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

typedef struct time { 
    int hours, minutes, seconds; 
} Time;
    
int printTimeInSeconds(int n,int,...){
    Time res;
    res.t1=time1.hours*3600+time1.minutes*60+time1.seconds;
    res.t2=time2.hours*3600+time2.minutes*60+time2.seconds;
    res.t3=time3.hours*3600+time3.minutes*60+time3.seconds;
    res.t4=time4.hours*3600+time4.minutes*60+time4.seconds;
      
    return res;
}
    
int main() { 
    Time time1, time2, time3, time4; 
    scanf("%d:%d:%d", &time1.hours, &time1.minutes, &time1.seconds); 
    scanf("%d:%d:%d", &time2.hours, &time2.minutes, &time2.seconds); 
    scanf("%d:%d:%d", &time3.hours, &time3.minutes, &time3.seconds); 
    scanf("%d:%d:%d", &time4.hours, &time4.minutes, &time4.seconds); 
    printTimeInSeconds(1, time1); 
    printTimeInSeconds(2, time1, time2); 
    printTimeInSeconds(3, time1, time2, time3); 
    printTimeInSeconds(4, time1, time2, time3, time4); 
    return 0; 
}

Input:

00:00:01 
01:00:00 
12:59:59 
00:01:00

Output:

1 
3601 
50400 
50460

I wrote the function part wrongly and please explain how to use stdarg in this function

c


Solution 1:[1]

Here is a link for stdarg with a well explained example.
Explanation for va_list And an explanation for va_arg, va_start and va_end

I used Visual Studio to Compile.
First you have to decide if your function should print or if you want the result.
I print the result and return it.
In the function printTimeInSeconds() create type va_list(ap) for va_start and va_arg
Initialize type va_list(ap) with va_start for future retrive of passed arguments with va_arg.
va_arg returns value of (ap) as type of second parameter(Time) and changes pointer(ap) to next Argument.

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

#pragma warning (disable:4996)

typedef struct {
    int hours, minutes, seconds;
} Time;

int printTimeInSeconds(int n_args,...) {
    int res = 0;
    va_list ap;
    va_start(ap, n_args);
    for (int i = 1; i <= n_args; ++i)
    {
        //Better solution from Jonathan Leffler
        Time t = va_arg(ap, Time);
        res += (t.hours * 60 + t.minutes) * 60 + t.seconds;
        /*My old solution
             //va_arg(ap, int) returns the value of type(int) and moves pointer(ap)+= size of type(int)
        res += va_arg(ap, int) * 3600;      //hours
        res += va_arg(ap, int) * 60;        //minutes
        res += va_arg(ap, int);             //seconds
             //va_arg(ap, int) type is always int because the struct is only int
        */
    }
    //va_end before return of function
    va_end(ap);
    //if you want your function to print
    printf("%d\n", res);
    //if you need the result
    return res;
}

int main() {
    Time time1, time2, time3, time4;
    scanf("%d:%d:%d", &time1.hours, &time1.minutes, &time1.seconds);
    scanf("%d:%d:%d", &time2.hours, &time2.minutes, &time2.seconds);
    scanf("%d:%d:%d", &time3.hours, &time3.minutes, &time3.seconds);
    scanf("%d:%d:%d", &time4.hours, &time4.minutes, &time4.seconds);
    printTimeInSeconds(1, time1);
    printTimeInSeconds(2, time1, time2);
    printTimeInSeconds(3, time1, time2, time3);
    printTimeInSeconds(4, time1, time2, time3, time4);
    return 0;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1