Structure with Pointers, Not getting any output with this code

Write your question here.
Here I am trying to use MyTime struct for calculating time between two time entered by users.

I am compiling it and it ask user to enter but not getting any output so please help me with this where I am doing something wrong.

Here is the instruction for code:

A function named computeTimeDifference that computes the time elapsed between the start and stop times stored in the two type MyTime structures pointed to by its two parameters, stores it in another MyTime structure, then returns a pointer to that structure.
Function computeTimeDifference must:
• Have only two parameters, both of data type “pointer to const MyTime”
• Not declare any pointers other than its two parameters
• Not declare any structures other than the one that will hold the elapsed time
• Return a “pointer to MyTime” that points to a MyTime structure containing the elapsed time

Here is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 #include <iostream>
using namespace std;

struct MyTime {int hours, minutes, seconds;};


MyTime *computeTimeDifference(const struct MyTime *t1, const struct MyTime *t2)
{
    static MyTime *difference;

    difference->seconds = (t2->hours*60*60 + t2->minutes*60 + t2->seconds) -
                          (t1->hours*60*60 + t1->minutes*60 + t1->seconds);

    if (difference->seconds >= 0)
    {
        //extract time in Hours, Minutes and Seconds
        difference->minutes = difference->seconds / 60;
        difference->hours = difference->minutes / 60;
        difference->minutes = difference->minutes % 60;
        difference->seconds = difference->seconds % 60;
    }
    else
    {
        difference->seconds += 86400;
        //extract time in Hours, Minutes and Seconds
        difference->minutes = difference->seconds / 60;
        difference->hours = (difference->minutes / 60);
        difference->minutes = difference->minutes % 60;
        difference->seconds = difference->seconds % 60;
    }
    return ((MyTime *)difference);
}

int main() {
    MyTime stopTime, startTime, *difference;
    
    printf("Enter the stop time. \n");
    printf("Enter hours, minutes and seconds: ");
    scanf("%d %d %d", &startTime.hours, &startTime.minutes, &startTime.seconds);

    printf("Enter the stop time. \n");
    printf("Enter hours, minutes and seconds: ");
    scanf("%d %d %d", &stopTime.hours, &stopTime.minutes, &stopTime.seconds);

    // Difference between start and stop time
    difference = computeTimeDifference(&startTime, &stopTime);
    printf("\nTime Difference: %d:%d:%d - ", startTime.hours, startTime.minutes, startTime.seconds);
    printf("%d:%d:%d ", stopTime.hours, stopTime.minutes, stopTime.seconds);
    printf("= %d:%d:%d\n", difference->hours, difference->minutes, difference->seconds);
    return 0;
}
Last edited on
Well you could write it in C++ instead of C.
1
2
3
4
5
MyTime computeTimeDifference(const MyTime &t1, const MyTime t2) {
    MyTime difference;
    ///
    return difference;
}

Fix everything it complains about, without changing this interface.
Hi Can you explain it in details, I am kinda new to C/C++.

It's assignment code and I need to pass start time and stop time as pointer that returns pointer to MyTime struct. I need to write this code in C++.
On line 9, you declare a pointer called difference.

However, nowhere do you give that pointer any value. So it's just pointing to a random memory address. So, when you try and write data to that memory, you have undefined behaviour - probably, causing your program to crash.

What you need to do is allocate some memory for a MyTime object, and set difference to point to that object.

Or, alternatively, use salem c's technique.
Last edited on
> Hi Can you explain it in details, I am kinda new to C/C++.
Well the first thing is to decide whether you want to be a "C" programmer or a "C++" programmer.

Anyone who tries to be a pick-and-mix C/C++ programmer usually ends up with some hotchpotch mess that is the worst of both languages.

So begin by always referring to the actual language you're trying to learn.

> It's assignment code and I need to pass start time and stop time as pointer that
> returns pointer to MyTime struct.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
MyTime *computeTimeDifference(const struct MyTime *t1, const struct MyTime *t2)
{
    static MyTime difference;
    //
    return &difference;
}

vs
MyTime *computeTimeDifference(const struct MyTime *t1, const struct MyTime *t2)
{
    MyTime *difference = new MyTime;
    //
    return difference;
}

Both would work.
Both are awful in modern C++.

Who (or what) out of date thing are you learning from?
Last edited on
Thanks salem.c It worked.

I am taking University course.
> I am taking University course.
I would supplement your education with lots of external sources.
Both are awful in modern C++.

I thought I'd elaborate on Salem C's point. The problems are (1) passing a raw pointer as a parameter is prone to bugs and (2) returning a raw pointer is prone to even more bugs.

When you pass a pointer as a parameter, there's always the possibility that someone will pass NULL, which will probably crash the program. You can avoid the bug by making the parameter a reference or const reference instead. It's much harder to create a "null reference" and if the caller does, then it's their fault, not yours.

The bigger problem is returning a raw pointer from a function. The calling code needs to know if the function can return NULL. More importantly, the caller needs to know how long the pointer is valid. The answer might be:
- Until the next call to the function, such as when returning a pointer to a static
- Until the caller deletes it, such as when returning an object that was newly created on the heap
- Until the same thread calls the function again.
- Probably others.

Usually, programmers fail to document the answer and the caller is left guessing, which leads to bugs.

You can avoid the problem completely by returning an instance of the object itself. That way the caller can store the result wherever they want for however long they want.

Salem C combined both points when he suggested this:
MyTime computeTimeDifference(const MyTime &t1, const MyTime t2)

This avoids both problems. The parameters are const references so it's very hard to pass in a null object, and the return value is an instance of the class, so you don't have to worry about how long it's valid.
Topic archived. No new replies allowed.