0

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?

raquela
  • 268
  • 2
  • 8
  • 3
    You forgot to enable warnings in your compiler. Passing a `char**` while an `int` is expected, isn't the best idea. – Gerhardh Feb 12 '18 at 10:59
  • 1
    Well, after adding the necessary headers I get several compiler warnings -- `calc` making integer from pointer without a cast, `int` expected but argument of type `char**`.... how about fixing those before you bother with the expected / observed result? (Voting to close as "too broad", could also be absence of a [mcve].) – DevSolar Feb 12 '18 at 11:00
  • This might help; https://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean –  Feb 12 '18 at 11:01
  • When I enabled warning I received those problems, but when I am changing int to char it is still something wrong. I have no idea how to fixed it. I think that I don't understand some basic stuffs, that was why I post a question here. – raquela Feb 12 '18 at 11:19
  • @raquela: [A good book](https://stackoverflow.com/questions/562303), perhaps? ;-) The point is that `c_date` / `c_hour` are of type `char *`, and `&c_date` / `&c_hour` are of type `char **`, but `calc( int, char * )` expects an `int` as first parameter and a `char *` as second parameter. You are doing wrong things, those can't give correct results. And without a clear statement of intention -- *what is your program expected to actually **do**?* -- it's hard to show "correct" code. – DevSolar Feb 12 '18 at 11:40
  • 1
    provide detail code for function calc. – Abhijit Pritam Dutta Feb 12 '18 at 13:19

1 Answers1

1

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

  • I have exact copy of main function. I also tried without "int argc", but then I receive "Too many arguments to function calc". – raquela Feb 12 '18 at 11:35