简体   繁体   English

进程以退出代码 139 结束(被信号 11:SIGSEGV 中断)--Clang

[英]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.此代码块将允许用户输入数据,output 将是日期所在的星期几。 I'm fairly new to C, just been learning for about 4 days so I'm not sure how to fix this error.我对 C 还很陌生,刚刚学习了大约 4 天,所以我不知道如何解决这个错误。 When the code is changed to output int thawy, the result is correct, however, printing the string 'divine' gives an error.当代码更改为 output int thawy 时,结果是正确的,但是打印字符串 'divine' 会出错。

The string 'divine' is the cause of the error.字符串 'divine' 是错误的原因。 Any Solutions?任何解决方案? I'm pretty sure the error is caused by an issue with memory.我很确定该错误是由 memory 的问题引起的。 Not too sure how to fix this, already tried the malloc function, might've used it wrong.不太清楚如何解决这个问题,已经尝试过 malloc function,可能用错了。

The variable "divine" is just a single character.变量“神”只是一个字符。 You cannot assign a string to it nor can your use the "%s" to display it.您不能为其分配字符串,也不能使用“%s”来显示它。 Change it to a "char const *" vice a "char".将其更改为“char const *”,而不是“char”。 A "char *" can point to a string and the "const" tells the compiler that the content cannot change. “char *”可以指向一个字符串,“const”告诉编译器内容不能改变。

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.或者,您可以使用“char god[10]”(最长字符串为 9,末尾的 NUL 字符为 1)并使用 strcpy function 复制字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM