【问题标题】:Error C2057 expected constant expression while using a constant使用常量时出现错误 C2057 预期常量表达式
【发布时间】:2013-10-02 09:38:31
【问题描述】:

这是我的问题,我一定是在这里遗漏了什么。

 const int nfft = 256 * 1024;
const float samplefrequency = 256.0 * 1024.0 ; // Hz

/* The buffer, spectral and data arrays for the FFT */
kiss_fft_cfg mybuff;
kiss_fft_cpx samples[nfft];
kiss_fft_cpx fftoutput[nfft];

/* The final, averaged spectrum */
double finalspec[nfft/2];

所以这是我的代码的一部分。

问题是我无法编译它,因为: “错误 C2057:表达式常量出席”第 16 行 - Kiss_fft_cpx 样本 [nfft]; “错误 C2057:需要常量表达式”

考虑到 nfft 是一个常数,我不明白有什么问题。

谢谢

【问题讨论】:

    标签: c arrays compiler-errors size


    【解决方案1】:

    考虑到 nfft 是 常数

    在 C 中,const 变量并不是真正的常量,更像是只读对象。因此,它们不能用于所有可以使用真正常量的地方(例如数组的大小)。

    也许您可以使用宏:

    #define NFFS (256 * 1024)
    

    顺便提一下,还有一个关于这个主题的 C 常见问题解答条目:I don't understand why I can't use const values in initializers and array dimensions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-02
      • 1970-01-01
      相关资源
      最近更新 更多