1

Though there are many threads on this matter, i cannot find one that addresses my question, apologies if im being thick. An important distinction is that my solution cannot use pointers. I've started the chapter on structures and the problem i am working on calls for a function that takes two arguments t1 and t2, which are two different times in the framework of a structure. ive reduced that code as much as possible, and am finding that the values of my struct items arent getting through to the function.

#include<stdio.h>

struct time
{
    int hour;
    int minute;
    int second;
};

struct time t1 = {3, 45, 15};
struct time t2 = {9, 44, 03};

int main (void)
{
    struct time elapsedTime(struct time t1,struct time t2);

    return 0;
}

struct time elapsedTime(struct time t1,struct time t2)
{
    printf ("%i:%i:%i\n", t1.hour, t1.minute, t1.second);
    printf ("%i:%i:%i\n", t2.hour, t2.minute, t2.second);

    return;
}
5
  • 3
    In main, you want elapsedTime(t1, t2); Commented Feb 25, 2015 at 21:06
  • 1
    In a real program, you'll probably want to pass the structure via a pointer. The way it's currently written implies a copy of the structure being made when passing it. Commented Feb 25, 2015 at 21:07
  • @DanielKamilKozar: For a small structure consisting of just 3 int members, passing by value is not unreasonable. Commented Feb 25, 2015 at 21:50
  • The line struct time elapsedTime(struct time t1,struct time t2); is a declaration of the function. It's not necessary if the full definition precedes main. Just as a matter of style, it's more common to put function declarations at file scope, outside any function. Commented Feb 25, 2015 at 21:52
  • @KeithThompson - of course. That's why I put "probably" in there. :) Commented Feb 25, 2015 at 21:52

4 Answers 4

2

You are declaring the function in main, but not calling it.

Instead of:

int main (void)
{
    struct time elapsedTime(struct time t1,struct time t2);

    return 0;
}

You need:

struct time elapsedTime(struct time t1,struct time t2);

int main (void)
{
    struct time mt = elapsedTime(t1, t2);

    // Do something with mt...

    return 0;
}

A more common way to return a "bulky" data item would be as a pointer:

typedef struct my_time {
    int hour;
    int minute;
    int second;
} my_time_t;

void elapsedTime(my_time_t t1, my_time_t t2, my_time_t *t3)
{
    t3->hour = ...;
    t3->minute = ...;
    t3->second = ...;

    ...
}

NOTE: I would strongly recommend avoiding a generic, common name such as time for your struct since it has the potential of conflicting with some system defined value (now or later). Avoid naming your own variables names like like time, file, string, etc...

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

9 Comments

am i able to make this function return a struct type?
@JerrySingh sure. I just didn't handle the return value in main in my example, but you can do that. See my edit of the answer.
Unfortunately, I can't use pointers yet. Also, the exercise calls for a function that takes two arguments. Im sure the restrictions are frustrating, if it helps ive been tearing my hair out over here.
@JerrySingh yep I was just indicating it's appropriate here, but also show the direct struct return case.
with the struct time mt = elapsedTime(struct time t1, struct time t2) i get an error: invalid initializer
|
2

You never called your function

elapsedTime( t1, t2 );

Comments

0

Most likely the behavior described is caused by hiding the global variables t1 and t2 with the parameters of identical name. (There are other problems with the code as written that others have covered).

I would change the argument names ,for example:

struct time t1 = {3, 45, 15};
struct time t2 = {9, 44, 03};

struct time elapsedTime(struct time time1,struct time time2)

Comments

0

I have found a solution:

#include<stdio.h>

struct time
{
  int hour;
  int minute;
  int second;
};

void elapsedTime(struct time t1, struct time t2)
{
  printf("%i:%i:%i\n", t1.hour, t1.minute, t1.second);
  printf("%i:%i:%i\n", t2.hour, t2.minute, t2.second);
}

int main()
{
  struct time t1 = { 3, 45, 15 };
  struct time t2 = { 9, 44, 03 };
  elapsedTime(t1, t2);
  return 0;
}

Compiles to the expected result for me.

As other have said, you need to call the function with just elapsedTime(t1, t2);.

EDIT:

To get what you said in your comment below with pointers, you can use the following concept:

struct time
{
  int hour;
  int minute;
  int second;
};

void elapsedTime(struct time t1, struct time t2, struct time *t3)
{
  printf("%i:%i:%i\n", t1.hour, t1.minute, t1.second);
  printf("%i:%i:%i\n", t2.hour, t2.minute, t2.second);
  t3->hour = 100;
  t3->minute = 50;
  t3->second = 25;
}

int main()
{
  struct time t1 = { 3, 45, 15 };
  struct time t2 = { 9, 44, 03 };
  struct time t3 = { 0, 0, 0 };

  elapsedTime(t1, t2, &t3);
  printf("%i:%i:%i\n", t3.hour, t3.minute, t3.second);
  return 0;
}

As you can see I passed a third struct to the function and change the struct values by reference in the function. (The values get changed outside of the function)

Then I print the struct outside of the function to check whether it worked.

Hope this will help when you can use pointers.

4 Comments

am i able to make the function return a type struct?
@JerrySingh You could but you're returning nothing in your function anyway, that's why I changed it to void.
true, id wanted to reduce the code as much as possible. the goal of the exercise is to pass the two struct types (in this case times of day) and have the function determine how much time has passed. My thinking was to have the parts subtract in the function, then return a t3 struct to main. im finding that whatever prefix i put to the function i get some sort of error. How do i return the struct from the function?
@JerrySingh I can only think of a way with pointers, for future references. Check out my updated answer.

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.