-3

I am getting a segmentation fault for some reason, I wrote this program that calculates the days between two dates and wanted to get the "dd-mm-yyyy" to be represented as a string and "dd2-mm2-yyyy2"should also be represented as a string, i thought I could solve it this way, but i cam getting a segmentation fault, can someone help me? what am i doing wrong?

  • 4
    Please spend some time in formatting your code before submitting a question. – Davide Spataro Sep 20 '19 at 12:48
  • 1
    What is your input, expected output, and where does the error occur? – Scott Hunter Sep 20 '19 at 12:52
  • 2
    Single-step through the program using your debugger. – Lundin Sep 20 '19 at 12:53
  • Apparently you didn't bother to debug step by step and even to check your code: why should we ? – Guillaume Petitjean Sep 20 '19 at 13:20
  • 1
    `atoi(argv[n])` without first checking if the required number of arguments have been passed is a very easy way to get seg-faults. Never access `argv[n]` before first ensuring `argc > n` – th33lf Sep 20 '19 at 13:23
  • Put code in function. Call that function with some example dates. Check the output. That way you can change your code and see if you break things. – qräbnö Sep 20 '19 at 14:21
  • its not that i didnt bother, i didnt even know what debbuger was until you mentioned iit, I am complete beginner, i took around 1 lesson so far, so it snot that I am not bothering, I just dont know how, but I was trying to figure it out and couldnt.. –  Sep 20 '19 at 15:08
  • the function: `atoi()` does not indicate when an error occurred. Suggest using `strtol()` or similar function as that family of functions does indicate when an error occurs – user3629249 Sep 20 '19 at 22:25
  • The posted code fails to check `argc` before accessing anything beyond `argv[0]`. and when there are not enough command line arguments then the code should output `USAGE:` message to `stderr` probably via a call to `fprintf( stderr, "USAGE: %s \n", argv[0] );` – user3629249 Sep 20 '19 at 22:28
  • regarding: `printf("Error - enter a month between the range ( 1 - 12)\n");` This says to enter a month that is in the range 2...11. Suggest: `printf("Error - enter a month in the range ( 1 - 12) inclusive\n");` – user3629249 Sep 20 '19 at 22:31
  • regarding: `char yearstr[5]; yearstr[0]= argv [1][6]; yearstr[1]=argv [1][9]; yearstr[2]= '\0'; yyyy= atoi(yearstr);` What is this expected to produce? What is the expected/typical input string? Why not use: `yyyy = (int)strtol( argv[1], NULL, 10 );` or similar – user3629249 Sep 20 '19 at 22:40
  • regarding: `if ((yyyy % 4 == 0) || ( yyyy % 100 == 0 ) && ( yyyy % 400 == 0 )){ monthLength[1] = 29;` and these two code blocks: `for(i=0; i – user3629249 Sep 20 '19 at 22:46

1 Answers1

1

This seems incorrect. argv[1] is your "day" string, which is 1 or 2 characters long, and you're indexing characters 3 and 4.

char monstr[3];
monstr[0]= argv [1][3];
monstr[1]=argv [1][4];
monstr[2] = '\0';

This should probably be:

char monstr[3];
monstr[0]= argv [2][0];
monstr[1]=argv [2][1];
monstr[2] = '\0';

Same with some other strings.

But, that said, I'm basing this on how you seem to be parsing input. If you want your input to be dd-mm-yyyy, then you're not getting input right. Instead, you should do something like this:

int dd, mm, yyyy;
sscanf(argv[1], "%d-%d-%d", &dd, &mm, &yyyy);

And same with the other string. And in that case, the previous thing I corrected doesn't need to be corrected.

As a general piece of advice: the reason segmentation faults happen is because you're accessing memory that you aren't able to access. A common cause of this is going outside of array bounds, or using invalid pointers. In your case, it seems like one of those two, and it comes from misuse of argv.

Kalinovcic
  • 492
  • 3
  • 11
  • This question seems to already be answered by [date-time conversion](https://stackoverflow.com/questions/95492/how-do-i-convert-a-date-time-to-epoch-time-unix-time-seconds-since-1970-in-per) – user3629249 Sep 20 '19 at 22:57
  • so is it going to be like this? char daystr[3]; daystr[0] =argv[1][0]; daystr[1] = argv[1][1]; daystr[2] = '\0'; dd = atoi(daystr); char monstr[3]; monstr[0]= argv [2][0]; monstr[1]=argv [2][1]; monstr[2] = '\0'; mm = atoi(monstr); char yearstr[5]; yearstr[0]= argv [3][0]; yearstr[1]=argv [3][1]; yearstr[2]= '\0'; yyyy = atoi(yearstr); char daystr2[3]; daystr2[0] =argv[4][0]; daystr2[1] = argv[4][1]; daystr2[2] = '\0'; dd2= atoi(daystr2); –  Sep 24 '19 at 00:21