0

This is program which turns seconds into secods-minutes-hours.When i try to compile it, I get one error "Invalid initializer" on "struct time_t malko=times(a);" please someone help P.S. I have to do it only with structs not pointers or anything else!!


#include <stdio.h>

  struct time_t {
  float sec;
  int min,hours;
};

struct time_t fastest_time (float times[3]){
     int i;
     int prom;
     struct time_t malko;
     for(i=0;i<3;i++) {
     if(prom>times[i]) {prom=times[i];}
}
    malko.sec=prom%60;
    prom=prom/60;
    malko.min=prom%60;
    prom=prom/60;
    malko.hours=prom%60;

   return malko;
}


int main () {
   float a[3]={3423,1234,34232};
   struct time_t malko=times(a);
   printf("\n %d %d %f", malko.hours, malko.min, malko.sec);

   return 0;
}
  • 2
    (1) What is `times` here `struct time_t malko=times(a);`? I don't see a function named `times` (2) `prom` is initialized in the first iteration of the `for` loop invoking UB – Spikatrix Jun 19 '17 at 12:07
  • 2
    You do know that `time_t` is a *standard* type? Even though structure tags (names) live in their own namespace, using the same name for your structure as a standard type will lead to confusion. – Some programmer dude Jun 19 '17 at 12:08
  • If you try to use a functions argument name as the actual function name, then you definitely and desperately [should find a good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and start all over from the beginning. If you use `times` because it's the argument name in the `fastest_time` function, then how would you attempt to call a function with two or more arguments? – Some programmer dude Jun 19 '17 at 12:12
  • On a totally unrelated note, please don't use leading newline with `printf`. Use *trailing* newline instead. First of all it will make sure that the output is actually written, and if you run from the command-line then the output will not be written on the same line as the prompt. – Some programmer dude Jun 19 '17 at 12:14
  • `time_t` is already used by the c library. You should find another name for your structure, I suggest using a standard type, such as `timeb`. – Michaël Roy Jun 19 '17 at 12:20

1 Answers1

3

struct time_t malko=fastest_time(a); instead of struct time_t malko=times(a); ?

times isn't defined for main. (here the output : http://codepad.org/yvGhoHw4 )

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Adrien B
  • 91
  • 5