aviv-1
aviv-1

Reputation: 1

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)--Clang

#include <stdio.h>
int mm;
int dd;
int yy;
int deez;
int thawy;
char divine;
int yearcode(int yy){
int a=(yy/4)+yy;
int b=a%7;
return b;}
int monthcode(int mm){
    if(mm==1||mm==10){
        deez=0;
    } if(mm==2||mm==3){
        deez = 3;
    }if(mm==4||mm==7){
        deez=6;
    } if(mm==5){
        deez=1;
    }if(mm==6){
        deez=4;
    } if(mm==8){
        deez=2;
    } if(mm==9||mm==12) {
        deez = 5;
    }return deez;}
    int main() {
  printf("Please enter your date in the form mm/dd/yy\nEx. 03/26/19\n:");
  scanf("%d/%d/%d",&mm,&dd,&yy);
if(mm==1||mm==2){
thawy=(yearcode(yy)+monthcode(mm)+6+dd+-1)%7;
}else{
    thawy=(yearcode(yy)+monthcode(mm)+6+dd)%7;
}
if(thawy==0){
     divine="Sunday";
}if(thawy==1){
     divine="Monday";
}if(thawy==2){
   divine="Tuesday";
}if(thawy==3){
     divine="Wednesday";
}
if(thawy==4){
     divine="Thursday";
}if(thawy==5){
   divine="Friday";
}else{
    divine="Saturday";
}

printf("%d/%d/%d,is on a %s",mm,dd,yy,divine);
  return 0;
}

This block of code would allow the user to input data, and the output would be what day of the week the date is on. I'm fairly new to C, just been learning for about 4 days so I'm not sure how to fix this error. When the code is changed to output int thawy, the result is correct, however, printing the string 'divine' gives an error.

The string 'divine' is the cause of the error. Any Solutions? I'm pretty sure the error is caused by an issue with memory. Not too sure how to fix this, already tried the malloc function, might've used it wrong.

Upvotes: 0

Views: 731

Answers (1)

Jim D
Jim D

Reputation: 316

The variable "divine" is just a single character. You cannot assign a string to it nor can your use the "%s" to display it. Change it to a "char const *" vice a "char". A "char *" can point to a string and the "const" tells the compiler that the content cannot change.

Alternatively, you can use "char divine[10]" (9 for the longest string and 1 for the NUL character at the end) and use the strcpy function to copy the string.

Upvotes: 1

Related Questions