2012-03-11 171 views
0

我无法找到将float * []类型的指针数组作为参数传递给函数的正确语法。在C++中传递指针数组作为参数

real * pp[] = { _osc[0].get_samples_ptr(), _osc[1].get_samples_ptr() }; 
_mod.iterate_sample_loop(samples, p_syn_ctx, pp); 

是好的,但

_mod.iterate_sample_loop(samples, p_syn_ctx, 
          { _osc[0].get_samples_ptr(), 
          _osc[1].get_samples_ptr() }); 

其中iterate_sample_loop是: “:预期前主表达式 'xxx' 的令牌错误”

void mod::iterate_sample_loop(u32 samples, 
           synth_context * p_syn_ctx, 
           real * p_inputs[]) ... 

结果。

回答

3

您不能用{ _osc[0].get_samples_ptr(), _osc[1].get_samples_ptr() }创建临时阵列。 AFAIK创建临时阵列的唯一方法是这样的:

typedef int array_type[3]; 

void f(int the_array[]) {} 

int main() { 
    f(array_type{1,3,4}); 
} 

虽然这只适用于C++ 11。我认为在C++ 03中完全不可能。

+0

我猜答案使用C++ 11统一初始化,而你不能用C++ 98实现同样的功能吗? – Suma 2012-03-11 20:33:08

+0

'typedef real * inputs [];' ... '_mod.iterate_sample_loop(samples,p_syn_ctx,inputs {_osc [0] .get_samples_ptr(),_osc [1] .get_samples_ptr()});' 给出同样的错误。 – 2012-03-11 20:35:18

+0

@Suma,是的,这是真的。 – 2012-03-11 20:37:00