免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6667 | 回复: 5

求助error: initializer element is not constant的错误,麻烦指点。 [复制链接]

论坛徽章:
0
发表于 2010-08-26 11:42 |显示全部楼层
论坛也有人讨论过这个错误,在http://bbs.chinaunix.net/thread-1285396-1-1.html中,
但没看懂。
我的test.c如下
  1. #include <stdio.h>

  2. typedef struct Admitem
  3. {
  4.     const char  *pcCommand;
  5.     int (*pFunc)(int, char**);
  6. } Admitem_t;

  7. static int Start(int iArgc, char *cArgv[])
  8. {
  9.     return 0;
  10. }

  11. static int Stop(int iArgc, char *cArgv[])
  12. {
  13.     return 0;
  14. }

  15. static int Show(int iArgc, char *cArgv[])
  16. {
  17.     return 0;
  18. }

  19. static Admitem_t strAdmitem[]=
  20. {
  21.     ("start", Start),
  22.     ("stop", Stop),
  23.     ("show", Show),
  24. };


  25. int main(int iArgc, char *cArgv[])
  26. {
  27.     return 0;
  28. }
复制代码
报错如下:

[root@localhost cmd]# gcc test.c
test.c:26: error: initializer element is not constant
test.c:26: error: (near initialization for 'strAdmitem[0].pcCommand')
test.c:27: error: initializer element is not constant
test.c:27: error: (near initialization for 'strAdmitem[0].pFunc')
test.c:28: error: initializer element is not constant
test.c:28: error: (near initialization for 'strAdmitem[1].pcCommand')


有没有人可以帮我指点下?

论坛徽章:
0
发表于 2010-08-26 11:46 |显示全部楼层
附上我的GCC信息

Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)

论坛徽章:
0
发表于 2010-08-26 11:50 |显示全部楼层
同学,你必须知道,一个结构体是用花括号 {} 而不是圆括号 () 来描述的。

论坛徽章:
0
发表于 2010-08-26 11:53 |显示全部楼层
同学,你必须知道,一个结构体是用花括号 {} 而不是圆括号 () 来描述的。
langue 发表于 2010-08-26 11:50



我哭了,居然犯了这么低级的错误。。。
谢谢楼上花费时间了来帮我定位。。

真惭愧。。。

论坛徽章:
0
发表于 2010-08-26 12:07 |显示全部楼层
这费啥时间啊。根本不用看源码。第一眼看到
test.c:26: error: initializer element is not constant

再看26 行,有()这个东东。于是行出为什出错。

总时间不超过二秒。

论坛徽章:
0
发表于 2010-08-26 12:11 |显示全部楼层
这费啥时间啊。根本不用看源码。第一眼看到
test.c:26: error: initializer element is not constant

...
zhangsuozhu 发表于 2010-08-26 12:07



是啊,没写码太久,基础都忘了,要补回来才行了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP