#include<stdio.h> void month_day(int year,int days,int *month,int *day){ int d[2][13] = { {0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31} }; int sum = 0,isRun; if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){ isRun = 1; } else{ isRun = 0; } for(int i=0;i<13;i++){ sum+=d[isRun][i]; if(sum>days){ *month = i-1; *day = days-sum+d[isRun][i]; printf("%d,%d",*month,*day); break; } } } int main(){ int *month,*day; month = day = NULL; int year,days; printf("请输入年份:\n"); scanf("%d",&year); printf("请输入天数:\n"); scanf("%d",&days); month_day(year,days,month,day); printf("今天日期是:%d:%d:%d",year,*month,*day); return 0; }这段代码哪里出错了
时间: 2023-06-24 13:09:13 浏览: 91
#include <stdio.h>
这段代码中出现了指针未初始化的问题,导致程序出错。具体来说,`month`和`day`指针未指向任何有效的内存地址,使用它们进行赋值操作会导致程序崩溃。为了解决这个问题,可以将`month`和`day`声明为普通的整型变量,并传入函数中进行赋值操作,如下所示:
```
#include<stdio.h>
void month_day(int year,int days,int *month,int *day){
int d[2][13] = { {0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31} };
int sum = 0,isRun;
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){ isRun = 1; }
else{ isRun = 0; }
for(int i=0;i<13;i++){
sum+=d[isRun][i];
if(sum>days){
*month = i-1;
*day = days-sum+d[isRun][i];
printf("%d,%d",*month,*day);
break;
}
}
}
int main(){
int month,day; // 修改这里
int year,days;
printf("请输入年份:\n");
scanf("%d",&year);
printf("请输入天数:\n");
scanf("%d",&days);
month_day(year,days,&month,&day); // 修改这里
printf("今天日期是:%d:%d:%d",year,month,day);
return 0;
}
```
另外,程序中还存在一处小问题,`printf("%d,%d",*month,*day)`后面应该加上换行符`\n`,否则输出结果会与后面的日期信息连在一起。
阅读全文