1

Im trying to get a grasp on how pointers work but I just cant put it togheter. I just want an impetus to know how to to proceed when I have the same problem again.

This is a programm that should add two different times, but the read in, add and subtract must be in different functions.

Within the functions everything works fine, but I just cant work out to send everything to the main().

I´ve tried implementing pointers in the arguments of the functions but when I do that everything gets really messy and nothing works, so im showing you a cleaner version of my code.

I would be really thankful if someone could help me with my understanding :)

PD: Thanks for being such an amazing community!

Here is my code:

#include <stdio.h>
#include <stdlib.h>

void subtracttimes (int hr0,int min0, int sek0, int hr1, int min1, int sek1, char op);
void addtimes ( int hr0, int min0, int sek0, int hr1, int min1, int sek1, char op );
char readin ();

int main( int hr0, int min0, int sek0, int hr1, int min1, int sek1, char op)
{   
    char op1;
    
    op1 = readin ();


    switch(op1){                           
            case '+': addtimes (hr0, min0, sek0, hr1, min1, sek1, op);    
                break;
            case '-': subtracttimes (hr0, min0, sek0, hr1, min1, sek1, op);
                break;
    }

    return 0;
} 



char readin () 
{
    int hr0, min0, sek0;
    int hr1, min1, sek1;
    char c, op, *merker;
    int ok;

    printf("Time Calculator\n");
    printf("================\n\n");

    printf("Start time (hh:mm:ss): ");
    ok = scanf("%d:%d:%d", &hr0, &min0, &sek0);
    while((c = getchar()) != '\n' && c != EOF) {}


    while ( ok != 3 || c != '\n' || hr0 > 23 || min0 > 59 || sek0 > 59) {
    printf("Wrong input!\n");
    printf("Start time (hh:mm:ss): ");
    ok = scanf("%d:%d:%d", &hr0, &min0, &sek0);
    while((c = getchar()) != '\n' && c != EOF) {}
    } 

    printf("Second time (hh:mm:ss): ");
    ok = scanf("%d:%d:%d", &hr1, &min1, &sek1);
    while((c = getchar()) != '\n' && c != EOF) {}

    while (ok != 3 || c != '\n' || hr1 > 23 || min1 > 59 || sek1 > 59) {
    printf("Wrong input!\n");
    printf("Second Time (hh:mm:ss): ");
    ok = scanf("%d:%d:%d", &hr1, &min1, &sek1);
    while((c = getchar()) != '\n' && c != EOF) {}
    } 

    printf("Type of operation (+/-): ");
    ok = scanf("%c", &op);
    while((c = getchar()) != '\n' && c != EOF) {}

    return op;

}

void addtimes ( int hr0, int min0, int sek0, int hr1, int min1, int sek1, char op )
{
    int resultHour;
    int resultMin;
    int resultSec;
    int resultDay = 0;

    resultHour = hr0 + hr1;
    resultMin = min0 + min1;
    resultSec = sek0 + sek1;

    if (resultSec > 59){
        resultMin += 1;
        resultSec -= 60;
    } else if (resultMin > 59){
        resultHour += 1;
        resultMin -= 60;
    } else if (resultHour > 23){
        resultDay += 1;
        resultHour -= 24;
    }

    printf("%02d:%02d:%02d %c %02d:%02d:%02d: = ", hr0, min0, sek0, op, hr1, min1, sek1);

    if(resultDay==0) {
        printf("%02d:%02d:%02d", resultHour, resultMin, resultSec);
    } else if (resultDay>0) {
        printf("%d Tag %02d:%02d:%02d", resultDay, resultHour, resultMin, resultSec);
    }


}

void subtracttimes (int hr0, int min0, int sek0, int hr1, int min1, int sek1, char op)
{
    int resultHour;
    int resultMin;
    int resultSec;
    int resultDay = 0;

    resultHour = hr0 - hr1;
    resultMin = min0 - min1;
    resultSec = sek0 - sek1;

    if (resultSec <= 0){
        resultMin -= 1;
        resultSec = 60 - (sek1 -sek0);
    } else if (resultMin <= 0){
        resultHour -= 1;
        resultMin = 60 - (min1 - min0);
    } else if (resultHour <= 0){
        resultHour = 60 - (hr1 - hr0);
    }

    printf("%02d:%02d:%02d %c %02d:%02d:%02d: = ", hr0, min0, sek0, op, hr1, min1, sek1);
    printf("%02d:%02d:%02d", resultHour, resultMin, resultSec);
}

Darius E.
  • 13
  • 5
  • First of all, you can't define `main` like that in a hosted environment. It has to be either `int main(void)` or `int main(int argc, char* argv[])`. You need to use the latter, if you want to pass values to your application via the command line. – Felix G Jul 01 '20 at 11:28
  • @FelixG Thanks for your answer. What do you mean exactly with latter? – Darius E. Jul 01 '20 at 11:36
  • What i meant is that you only need the second variant of `main`, if you plan to pass values to the application as command line arguments. But since that doesn't seem to be the case here, `int main(void)` is sufficient. – Felix G Jul 01 '20 at 11:39

2 Answers2

1

You can add some output parameters as you want multiple outputs to be filled in the function. For example you can modify the function so it looks like:

void addtimes ( int hr0, int min0, int sek0, int hr1, int min1, int sek1, char op, int* hr_res, int* min_res, int* sek_res) {
    ...
    *hr_res = your_calculated_hr_res;
    *min_res = your_calculated_min_res;
    *sek_res = your_calculated_sek_res;
    ...
}

In main, you have to add some variables to pass into the function to get the results you want.

int main() {
     int hr_res, min_res, sek_res;
     addtimes(..., &hr_res, &min_res, &sek_res);
}
bishopp
  • 113
  • 8
0

In C all parameter calls are by value. You can simulate call by reference by sending a pointer but in reality you still make a call by value as the pointer value is copied to the function. Normally you get values from a function by returning a value but when sending pointers you actually update the value that resides outside of the function, See Does C even have "pass by reference"?

Serafim
  • 484
  • 3
  • 10