| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6031 人关注过本帖
标题:error: initializer element is not constant. 错误
只看楼主 加入收藏
ebony
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2011-1-26
结帖率:55.56%
收藏
 问题点数:0 回复次数:1 
error: initializer element is not constant. 错误
程序编译的时候,遇到一个错误:

error: initializer element is not constant.

经检查, 是因为头文件里定义了一个常量:
static const int aaa = 25;

在C文件里, 定义了一个全局变量:
int test = aaa;

请问, 为什么会出现这个错误呢?

我的理解, aaa在编译时间内可以得到确定的值25. 同样在编译时间里, 将之付给另外的变量,会不容许吗?

我用的编译器是GCC.
搜索更多相关主题的帖子: 编译器 test 时间 
2011-01-26 00:51
ebony
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2011-1-26
收藏
得分:0 
算了,看来高手都在睡觉啊.

从英文网站找到了原因.

initializer element is not a constant
    In C, global variables can only be initialized with constants, such as numeric values, NULL or fixed strings. This error occurs if a non-constant value is used. Example:

    #include <stdio.h>

    FILE *stream = stdout;  /* not constant */
    int i = 10;
    int j = 2 * i;          /* not constant */

    int
    main (void)
    {
      fprintf (stream, "Hello World!\n");
      return 0;
    }

    This program attempts to initialize two variables from other variables.
2011-01-26 01:30
快速回复:error: initializer element is not constant. 错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016800 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved