这是原始代码
#include <stdio.h>
int main()
{
int y,m;
int s[12];
scanf("%d %d",&y,&m);
if((y%4==0&&y%100!=0)||(y%400==0))
s[12]={31,29,31,30,31,30,31,31,30,31,30,31};
else
s[12]={31,29,31,30,31,30,31,31,30,31,30,31};
printf("%d",s[m-1]);
return 0;
}
在对数组进行赋值的时候出现的这样的错误
/tmp/compiler_lf42y8wv/src: 在函数‘int main()’中: /tmp/compiler_lf42y8wv/src:8:51: 错误:cannot convert ‘<花括号内的初始值列表>’ to ‘int’ in assignment 8 | s[12]={31,29,31,30,31,30,31,31,30,31,30,31}; | ^ /tmp/compiler_lf42y8wv/src:10:51: 错误:cannot convert ‘<花括号内的初始值列表>’ to ‘int’ in assignment 10 | s[12]={31,29,31,30,31,30,31,31,30,31,30,31}; | ^
原因是:当数组的定义与初始化分开时,只能进行单个元素的赋值,不能进行统一赋值。
也就是说,在以后的赋值时,需要写为文章来源:https://www.toymoban.com/news/detail-758467.html
#include <stdio.h>
int main()
{
int y,m;
int s[12];
scanf("%d %d",&y,&m);
if((y%4==0&&y%100!=0)||(y%400==0))
s[0]=31;
s[1]=29;
s[2]=31;
else
s[0]=31;
s[1]=29;
s[2]=31;
printf("%d",s[m-1]);
return 0;
}
或者简单一点写为文章来源地址https://www.toymoban.com/news/detail-758467.html
#include <stdio.h>
int main()
{
int y,m;
int s[12]={31,29,31,30,31,30,31,31,30,31,30,31};
scanf("%d %d",&y,&m);
if((y%4==0&&y%100!=0)||(y%400==0))
s[1]=29;
else
s[1]=28;
printf("%d",s[m-1]);
return 0;
}
到了这里,关于错误:cannot convert ‘ ’ to ‘int’ in assignment的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!