期望的常数表达式 [英] Expected constant expression

查看:123
本文介绍了期望的常数表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有如下的C代码片段.任何解决方案都会导致所有声明都已声明并初始化

We have a snippet of C codes as below. Any solution cause all of it have been declared and initialize

void fillProcess(void *unused, Uint8 *stream, int len)
{
    Uint8 *waveptr;
    int waveleft=0;

    waveptr = wave.sound + wave.soundpos;
    waveleft = wave.soundlen - wave.soundpos;
    while ( waveleft <= len ) {
    /* Process samples */
    Uint8 process_buf[waveleft];
    SDL_memcpy(process_buf, waveptr, waveleft);
    /* do processing here, e.g. */
    /* processing the audio samples in process_buf[*] */
    // play the processed audio samples
    SDL_memcpy(stream, process_buf, waveleft);
    stream += waveleft;
    len -= waveleft;
    // ready to repeat play the audio
    waveptr = wave.sound;
    waveleft = wave.soundlen;
    wave.soundpos = 0;
    }
}

出现以下3个错误

Error   1   error C2057: expected constant expression   
Error   2   error C2466: cannot allocate an array of constant size 0    
Error   3   error C2133: 'process_buf' : unknown size

推荐答案

Uint8 process_buf[waveleft];

此行使用C99中引入的可变长度数组.但是根据您的错误代码,您正在使用Visual Studio,它目前还不支持C99.

This line uses variable length array, which is introduced in C99. But according to your error code, you are using Visual Studio, which doesn't support C99 yet.

假设编译器仍然是Visual Studio,则可以动态分配 process_buf .

Assuming that the compiler is still Visual Studio, you can allocate process_buf dynamically.

这篇关于期望的常数表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆