【发布时间】:2018-04-01 03:44:33
【问题描述】:
我有一个由双精度数组成的数组,我需要向下舍入并转换为整数,以便可以将它们用作输出数组中的索引。我刚刚开始C 编程,但不确定它是如何工作的。到目前为止,我能想到的最好的方法是:
int create_hist( double input_array[], int count, int output_array[17] ) {
for ( int i = 0; i < count; i++ ) {
input_array[i] = int floor(input_array[i]);
output_array[input_array[i]]++;
但是,我收到以下我无法解读的错误:
array.c:11:20: error: expected expression before ‘int’
input_array[i] = int floor(input_array[i]);
^
array.c:12:7: error: array subscript is not an integer
hist[input_array[i]]++;
^
array.c:14:1: error: control reaches end of non-void function [-Werror=return-type]
}
^
如果有人能告诉我哪里出错了,我将不胜感激。
【问题讨论】:
-
你需要使用括号进行强制转换,你不能使用
float作为数组索引,你的函数被定义为返回int但从不返回任何东西。 -
int index = input_array[i]会将double隐式转换为int。无需强制转换或floor函数。 -
别忘了接受其中一个答案