0

I have a struct array (Training data[10]) that contains some data that I want to pass to functions.

int convertTime(Time time)
{

    minutes = time.seconds * 60;
    // Takes data from data[0].time.seconds and converts them to minutes. 
    // My only problem is that I am being asked send a struct to this function, but I have to send the array because that's where all my data is stored

    return minutes;
}

typedef struct
{
    int seconds;
} Time;

typedef struct
{
    Time time;
    double distance;
 } Training;

 Training data[10];

 Training input;

 scanf("%d %lf", input.time.seconds, input.distance);

 data[0].time.seconds = input.time.seconds;
 data[0].distance = input.distance;

So now data[0].time.seconds and data[0].distance contains all data I need. I just have to pass data[0].time.seconds to the function, but in my assignment I am prompted to send the struct Time to the function, and I don't understand that since Time is only storing temporary data? It's the stored data that I want to send to the function.

How do I convert seconds to hours, minutes and seconds?

time.hours = seconds / 3600;
time.minutes = (seconds - time.hours * 3600) / 60;
time.seconds = seconds - 3600 * time.hours - 60 * time.minutes;

This seems to be right in my eyes but it fails. hours is correctly calculated but not minutes and sconds :(

2
  • You can always initialise struct time to a useful value. Commented Nov 12, 2013 at 22:35
  • Can you please explain? Commented Nov 12, 2013 at 22:47

2 Answers 2

1

To pass the structure, name it in the call:

 some_function(data[0].time);   // By value
 other_function(&data[0].time); // By address

Both functions get passed the Time value contained in the data[0] element of your array of Training structures.

Sign up to request clarification or add additional context in comments.

9 Comments

I want to return an int from the function so I am typing int function(data[0].time){blablabla} and I get the error: error: cannot convert 'Time' to 'int' in initialization and error: expected ',' or ';' before '{' token
If you want to get an int back from the function, then you might write: int x = function(data[0].time); to call the function, but you'd write int function(Time t) { blahblahblah...return (int_expr); } to define the function. (Note that the h's in 'blah' are very important! :-D)
Wut? I have to define functions? I don't understand :( blah
In your code, you have a function int convertTime(Time time) { ... } which is a function definition. It needs to come after the definition of the type Time (so the code as written won't compile). And you need to fix it to divide the time in seconds by 60 to get the time in minutes, instead of multiplying. When you call the function, you can write: int mins = convertTime(date[0].time); which defines a variable mins and initializes it with the result of calling convertTime(). Or you can have a variable previously defined int x; and then do x = convertTime(date[0].time);.
Or you could write, for example, printf("Minutes: %d\n", convertTime(date[0].time)); to print the value. You need either the definition of convertTime() to appear before it is used, or you need a prototype visible before it is used. C99 and C11 require this; C89 (C90) did not when the return type was int; C++ always has required this.
|
0

Suppose you have a value which is the number of seconds since midnight. And suppose you define another structure with hours/minutes/seconds, you can set this clock structure as follows,

typedef struct
{
    int hours;
    int minutes;
    int seconds;
} Clock;

You can print this structure, either to a char buffer, or to stdout,

char*
clockPrint(Clock* timep,char *stringbuf)
{
    if(stringbuf)
        sprintf(stringbuf,"%02d:%02d:%02d",(timep)->seconds,(timep)->minutes,(timep)->seconds);
    else
        printf("%02d:%02d:%02d",(timep)->seconds,(timep)->minutes,(timep)->seconds);
    return stringbuf;
}

Extracting hours, minutes, and seconds from an epoch time or a number of seconds since midnight can be done,

int //return days...
TimeSet(Clock* timep, int epoch)
{
    (timep)->seconds = (epoch) % 60;
    (timep)->minutes = (epoch/60) % 60;
    (timep)->hours   = (epoch/60/60) % 24;
    int days;
    return days   = (epoch/60/60/24);
}

Should you want to obtain hours, minutes, or seconds from this clock value,

void
TimeGet(Clock* timep, int* hoursp, int* minutesp, int* secondsp)
{
    if(hoursp)   *hoursp   = (timep)->hours;
    if(minutesp) *minutesp = (timep)->minutes;
    if(secondsp) *secondsp = (timep)->seconds;
    return;
}

Since you have stored a Time in your Date struct, which contains a number of seconds (presumably since midnight), and you have an array of some number of these Date's defined,

Training data[10];
Training input;

You can use scanf to read your seconds and distance values. And as stated, you can then place your input into your data[0] element,

//scanf wants pointers to int and float data
float distance;
printf("enter: seconds distance "); fflush(stdout);
scanf("%d %lf", &(input.time.seconds), &distance);
//you can then store the distance into your input struct double
input.distance = distance;

data[0].time.seconds = input.time.seconds;
data[0].distance = input.distance;

You could also use gettimeofday(3) or clock_gettime(2) to grab the current time (seconds since epoch),

struct timeval tv;
gettimeofday(&tv,NULL);    //posix.1-2001 function, seconds
input.time.seconds = tv.tv_sec;
//or
struct timespec ts;
clock_gettime(CLOCK_REALTIME,&ts); //posix.1-2008 function, seconds
input.time.seconds = ts.tv_sec;

Then you can separate your seconds into hours, minutes, and seconds,

Clock clk;
int hours, minutes, seconds;
TimeSet(&clk, data[0].time.seconds);
TimeGet(&clk, &hours, &minutes, &seconds);

Or you can format a string for printing, or print to stdout,

char clockbuffer[30];
clockPrint(&clk,NULL);
printf("time (HH:MM:SS): %s\n", clockPrint(&clk,clockbuffer));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.