warning: excess elements in scalar initializer [enabled by default] };

doodlesomething 2014-09-02 11:25:19
今天在完成K&R的习题 “练习 5-9 用指针方式代替数组下标方式改写函数 day_of_year 和 month_day”。时编译文件时遇到如下错误,鼓捣了好久都不知怎么解决,麻烦大家指点。先谢谢了!!!
warning: excess elements in scalar initializer [enabled by default] };


代码如下:


#include <stdio.h>


int main() {
int year,month,day,yearday;
int dayofyear(int year,int month,int day);
int monthofyear(int year,int total_day,int *month,int *day);
char *month_name(int n);
printf("%d\n",dayofyear(2014,9,2));
monthofyear(2014,246,&month,&day);
printf("%d(%s),%d\n",month,month_name(month),day);
return 0;
}


static int *daytab = {
31,
31+28,
31+28+31,
31+28+31+30,
31+28+31+30+31,
31+28+31+30+31+30,
31+28+31+30+31+30+31,
31+28+31+30+31+30+31+31,
31+28+31+30+31+30+31+31+30,
31+28+31+30+31+30+31+31+30+31,
31+28+31+30+31+30+31+31+30+31+30,
31+28+31+30+31+30+31+31+30+31+30+31,
31,
31+29,
31+29+31,
31+29+31+30,
31+29+31+30+31,
31+29+31+30+31+30,
31+29+31+30+31+30+31,
31+29+31+30+31+30+31+31,
31+29+31+30+31+30+31+31+30,
31+29+31+30+31+30+31+31+30+31,
31+29+31+30+31+30+31+31+30+31+30,
31+29+31+30+31+30+31+31+30+31+30+31
};

int leap(int year) {
return ( ( (year%4 == 0) && year%100 != 0 ) || year % 400 == 0 );
}

int dayofyear(int year,int month,int day) {
int leap(int year);
return *(daytab + ( (month - 1) + !leap(year) * 11) ) + day;
}

int monthofyear(int year,int yearday,int *month,int *day) {
int m,ly;

ly = leap(year);

if(yearday < 1 || yearday<(355+!ly)) {
return -1;
}

m = ly ? 11 : 23;

while(yearday < *(daytab + m)) {
m--;
}

if(month) {
*month = m % 12 + 1;
}

if(day) {
*day =yearday - (*(daytab + m));
}

return 0;
}

//the function will return the pointer of the month of sting
char *month_name(int n) {
static char *name[]={"January","February","March",
"April","May","June","July","August","September","October","November",
"December"};
return name[n-1];
}


...全文
2042 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
熊熊大叔 2014-09-03
  • 打赏
  • 举报
回复
应该是 static int *daytab = [ ... ] 而不是 static int *daytab = { ... }
doodlesomething 2014-09-03
  • 打赏
  • 举报
回复
引用 5 楼 lisong694767315 的回复:
[quote=引用 3 楼 doubleselect 的回复:] [quote=引用 2 楼 lisong694767315 的回复:] 把 static int *daytab 改成
 static int daytab[]
试试
按照你的做法,修改可以运行(先表示感谢),但我还有一个问题:我想定义的是一个指针数组,把static int *daytab 改为static int *daytab[] ={.....} 为什么还是报同样的错呢?我的思路有什么问题吗?为什么上面代码中的
//the function will return the pointer of the month of sting
    char *month_name(int n) {
        static char *name[]={"January","February","March",
        "April","May","June","July","August","September","October","November",
        "December"};
        return name[n-1];
    }
static char *name[]却可以呢?(我之前测试过了)。 谢谢![/quote] 你要搞清楚static int daytab[]中的 daytab 是一个一维数组,每个元素是一个整数。而 static char *name[] 你可以看作是一个二维的字符数组,其中每个元素是一个字符串(还是一个字符数组)。区分开一维和二维。 或者你可以这么看 static char *name[] 这个数组中的元素是 char*指针,每个指针指向一个字符串的首地址。。。 [/quote] 想明白了,谢谢!
神奕 2014-09-03
  • 打赏
  • 举报
回复
引用 3 楼 doubleselect 的回复:
[quote=引用 2 楼 lisong694767315 的回复:] 把 static int *daytab 改成
 static int daytab[]
试试
按照你的做法,修改可以运行(先表示感谢),但我还有一个问题:我想定义的是一个指针数组,把static int *daytab 改为static int *daytab[] ={.....} 为什么还是报同样的错呢?我的思路有什么问题吗?为什么上面代码中的
//the function will return the pointer of the month of sting
    char *month_name(int n) {
        static char *name[]={"January","February","March",
        "April","May","June","July","August","September","October","November",
        "December"};
        return name[n-1];
    }
static char *name[]却可以呢?(我之前测试过了)。 谢谢![/quote] 你要搞清楚static int daytab[]中的 daytab 是一个一维数组,每个元素是一个整数。而 static char *name[] 你可以看作是一个二维的字符数组,其中每个元素是一个字符串(还是一个字符数组)。区分开一维和二维。 或者你可以这么看 static char *name[] 这个数组中的元素是 char*指针,每个指针指向一个字符串的首地址。。。
doodlesomething 2014-09-03
  • 打赏
  • 举报
回复
引用 1 楼 truelance 的回复:
应该是 static int *daytab = [ ... ] 而不是 static int *daytab = { ... }
声明是用 {}没错的吧。
doodlesomething 2014-09-03
  • 打赏
  • 举报
回复
引用 2 楼 lisong694767315 的回复:
把 static int *daytab 改成
 static int daytab[]
试试
按照你的做法,修改可以运行(先表示感谢),但我还有一个问题:我想定义的是一个指针数组,把static int *daytab 改为static int *daytab[] ={.....} 为什么还是报同样的错呢?我的思路有什么问题吗?为什么上面代码中的
//the function will return the pointer of the month of sting
    char *month_name(int n) {
        static char *name[]={"January","February","March",
        "April","May","June","July","August","September","October","November",
        "December"};
        return name[n-1];
    }
static char *name[]却可以呢?(我之前测试过了)。 谢谢!
神奕 2014-09-03
  • 打赏
  • 举报
回复
把 static int *daytab 改成
 static int daytab[]
试试

70,019

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧