raquela
raquela

Reputation: 268

Writing functions in C, passing arguments

I found a code and I want to use it. When I run it from a terminal by ./code 20181010 0810, it works perfectly. I was trying to rewrite this code into function. The main code was declared by

int main (int argc, char *inp[]) { //some calculations }

So, I changed it into:

int calc(int argc, char *inp[]) { //some calculations }

and the write main code with additional calculations:

int calc(int argc, char *inp[]);
int main(int argc, char *inp[]) {

    char* c_date;
    char* c_hour;
    time_t timer;
    char buffer1[26], buffer2[26];
    struct tm* tm_info;

    time(&timer);
    tm_info = localtime(&timer);
    strftime(buffer1, 26, "%Y%m%d", tm_info);
    c_date = buffer1;
    strftime(buffer2, 26, "%H%M", tm_info);
    puts(buffer2);
    c_hour = buffer2;

    calc(&c_date, &c_hour);

    return 0;
}

And for example, for the time now 20180212 1045 it gives me 201802112355, when it should give me 201802121050.

What can be wrong?

Upvotes: 0

Views: 75

Answers (1)

user9251303
user9251303

Reputation:

At present you’ve just copied the main prototype. What does the function body of calc do? If you had an exact copy of the main function then...

int calc(int argc, char *inp[]);

argc is the number of arguments being passed into your program from the command line and inp is the array of arguments.

You’re passing in &c_date as argc

But that really depends what’s within the calc function......

Upvotes: 1

Related Questions