Jerry Singh
Jerry Singh

Reputation: 71

Difficulty passing struct to a function

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

Upvotes: 1

Views: 97

Answers (4)

LoLei
LoLei

Reputation: 437

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.

Upvotes: 0

lurker
lurker

Reputation: 58324

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...

Upvotes: 2

skimon
skimon

Reputation: 1189

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)

Upvotes: 0

P0W
P0W

Reputation: 47844

You never called your function

elapsedTime( t1, t2 );

Upvotes: 2

Related Questions