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?

6
  • 3
    You forgot to enable warnings in your compiler. Passing a char** while an int is expected, isn't the best idea. Commented Feb 12, 2018 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 minimal reproducible example.) Commented Feb 12, 2018 at 11:00
  • This might help; stackoverflow.com/questions/3024197/… Commented Feb 12, 2018 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. Commented Feb 12, 2018 at 11:19
  • 1
    provide detail code for function calc. Commented Feb 12, 2018 at 13:19

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I have exact copy of main function. I also tried without "int argc", but then I receive "Too many arguments to function calc".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.