开发者

Objective C -- passing array literal to a method

开发者 https://www.devze.com 2022-12-23 17:21 出处:网络
开发者_运维百科This seems to work (compiler doesn\'t complain, anyway): float adsr[4] = {0,1.0/PULSE_SPEED, 0,1};
开发者_运维百科

This seems to work (compiler doesn't complain, anyway):

float adsr[4] = {0,1.0/PULSE_SPEED, 0,1};
[sequence setBaseADSR:adsr];

but I want to make it more concise and do this:

[sequence setBaseADSR:{0,1.0/PULSE_SPEED, 0,1}];

How do I do it? In javascript, I'd call stuff in the brackets an "array literal". Not sure if C languages have the same concept or terminology though.


If your compiler supports the C99 compound literal syntax, it's possible.

[sequence setBaseADRS:(float [4]){0,1.0/PULSE_SPEED,0,1}];


The second way does not work because the compiler does not know which type the array elements are. However, this or something like this should work:

[sequence setBaseADSR:(float adsr[4] = {0,1.0/PULSE_SPEED, 0,1})];

as a declaration returns the leftmost element in the expression (cannot test it right now though)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号