Why can't I pass constant arrays as arguments?

南楼画角 提交于 2019-11-28 00:57:22

Short answer: You need to make use of a compound literal. Something like

 arrayfn( (double[]) {1.0, 2.0, 3.0} );

should do the job.


Some explanation

Coming to the part, why arrayfn({1.0, 2.0, 3.0}); did not work, because, without the syntax for compound literals, {1.0, 2.0, 3.0} is a brace enclosed initializer list. It does not denote an "object" which can be used as function argument. They are not "constant arrays", as you may have thought.

To add some more info, quoting C11, chapter §6.5.2.5, Compound literals

A postfix expression that consists of a parenthesized type name followed by a brace-enclosed list of initializers is a compound literal. It provides an unnamed object whose value is given by the initializer list.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!