定义有什么问题?

Ian*_*ker -1 c arrays

我正在尝试使用Windows10上的Visual Studio VC++ 2015编译"libsamplerate.dll",请参阅:http://www.mega-nerd.com/SRC/win32.html.

然后,我得到以下错误:

termination_test.c
.\tests\termination_test.c(82): error C2057: expected constant expression
.\tests\termination_test.c(82): error C2466: cannot allocate an array of constant size 0
.\tests\termination_test.c(82): error C2133: 'in': unknown size
.\tests\termination_test.c(83): error C2057: expected constant expression
.\tests\termination_test.c(83): error C2466: cannot allocate an array of constant size 0
.\tests\termination_test.c(83): error C2133: 'out': unknown size
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\cl.exe"' : return code '0x2'
Run Code Online (Sandbox Code Playgroud)

"termination_test.c"最初来自:http: //www.mega-nerd.com/SRC/download.html ,这里是导致错误的函数:

static void
simple_test (int converter)
{
    int ilen = 199030, olen = 1000, error ;

    {
        float in [ilen] ;
        float out [olen] ;
        double ratio = (1.0 * olen) / ilen ;
        SRC_DATA src_data =
        {   in, out,
            ilen, olen,
            0, 0, 0,
            ratio
        } ;

        error = src_simple (&src_data, converter, 1) ;
        if (error)
        {   printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
            exit (1) ;
            } ;
    } ;

    return ;
} /* simple_test */
Run Code Online (Sandbox Code Playgroud)

我只是将这两行修改为:

        float in [199030] ;
        float out [1000] ;
Run Code Online (Sandbox Code Playgroud)

......然后,工作得很好.

但是,定义有什么问题?

int ilen = 199030, olen = 1000, error ;
Run Code Online (Sandbox Code Playgroud)

我把'const'放在int前面,然后我得到了另一个错误"C2166:l-value指定const对象".我怎样才能使它没有错误?

(PS,这是一个开源代码,所以不应该有任何错误.这不是一个问题,但我只是想知道为什么.)

250*_*501 6

Visual Studio不支持可变长度数组.

定义数组大小的变量ilen和olen不是常量表达式(从这里描述为:常量),因此以下是可变长度数组:

float in [ilen] ;
float out [olen] ;
Run Code Online (Sandbox Code Playgroud)

值199030和1000是常量,因此以下只是常规数组:

float in [199030] ;
float out [1000] ;
Run Code Online (Sandbox Code Playgroud)

使用const限定符定义对象不会使对象成为常量.

您可以使用#define将值定义为宏,但这与手动编写常量相同,因为在使用它们表示的值编译代码之前替换定义的宏.

#define ILEN 1000
int array[ILEN];
Run Code Online (Sandbox Code Playgroud)

转换为:

int array[1000];
Run Code Online (Sandbox Code Playgroud)

在编译开始之前.

当然你应该使用#define它,因为它更方便,更不容易出错.