简体   繁体   English

C ++ - 用float索引数组时会发生什么?

[英]C++ - What happens when you index an array by a float?

I'm trying to understand exactly what happens when indexing through an array with a float value. 我试图准确理解在使用float值索引数组时会发生什么。

This link: Float Values as an index in an Array in C++ 此链接: Float Values作为C ++中Array的索引

Doesn't answer my question, as it states that the float should be rounded to an integer. 不回答我的问题,因为它声明浮点数应舍入为整数。 However in the code I'm trying to evaluate, this answer does not make sense, as the index value would only ever be 0 or 1. 但是在我试图评估的代码中,这个答案没有意义,因为索引值只能是0或1。

I'm trying to solve a coding challenge posted by Nintendo. 我正在尝试解决任天堂发布的编码挑战。 To solve the problem there is an archaic statement that uses a bitwise assignment into an array using a long complicated bitwise expression. 为了解决这个问题,有一个古老的语句,它使用一个长复杂的按位表达式对数组进行逐位赋值。

The array is declared as a pointer 该数组被声明为指针

unsigned int* b = new unsigned int[size / 16]; // <- output tab

Then it's assigned 0's to each element 然后它为每个元素分配0

for (int i = 0; i < size / 16; i++) {   // Write size / 16 zeros to b
    b[i] = 0;
}

Here's the beginning of the statement. 这是声明的开头。

b[(i + j) / 32] ^= // some crazy bitwise expression

The above sits inside of a nested for loop. 以上内容位于嵌套for循环内部。

I'm sparing a lot of code here, because I want to solve as much of this problem on my own as possible. 我在这里节省了很多代码,因为我想尽可能多地解决这个问题。 But I'm wondering if there is a situation were you would want to iterate through an array like this. 但我想知道是否有一种情况你想要迭代这样的数组。

There must be more to it than the float just automatically casting to an int. 它必须比浮动只是自动转换为int更多。 There hast to be more going on here. 这里有更多的事情要发生。

There are no float s here. 这里没有float size is an integer, and 16 is an integer, and consequently size/16 is an integer as well. size是整数, 16是整数,因此size/16也是整数。

Integer division rounds towards zero, so if size is in [0,16) , then size/16 == 0 . 整数除法向零[0,16) ,因此如果size[0,16) ,则size/16 == 0 If size is in [16,32) , then size/16 == 1 , and so on. 如果size[16,32) ,那么size/16 == 1 ,依此类推。 And if size is in (-16, 0] , then size / 16 == 0 as well. 如果size(-16, 0] ,那么size / 16 == 0

( [x,y) is the "half-open" interval from x to y : that is, it contains every number between x and y , and furthermore it includes x but excludes y ) [x,y)是从xy的“半开”间隔:也就是说,它包含xy之间的每个数字,而且它包含x但不包括y

The subscript operator in terms of arrays is syntactic sugar. 数组中的下标运算符是语法糖。 When you have the following : 如果您有以下内容:

class A {...}; 
A ar[17]; 
std::cout << ar[3] << std::endl;

Saying ar[3] is no different than saying : ar[3]与说:

*(ar + 3); 

So ar[3.4] is the same as saying 因此ar[3.4]与说法相同

*(ar + 3.4)    (1)

From the C++ Standard section 5.7.1 - Additive operators we read that : 从C ++标准第5.7.1节 - 添加运算符我们读到:

(...) For addition, either both operands shall have arithmetic or unscoped enumeration type, or one operand shall be a pointer to a completely-defined object type and the other shall have integral or unscoped enumeration type. (...)另外,两个操作数都应具有算术或非范围的枚举类型, 或者一个操作数应是指向完全定义的对象类型的指针另一个操作数应具有整数或无范围的枚举类型。

that's why expression (1) causes compilation error . 这就是表达式(1)导致编译错误的原因。

So, when you index an array by a float you get a compilation error 因此,当您通过浮点索引数组时,您会收到编译错误

To answer the question in the title: 要回答标题中的问题:

#include <stdio.h>
int main(int argc, char** argv) {
  int x[5];

  int i;
  for (i = 0; i < 5; ++i)
    x[i] = i;


  x[2.5] = 10;

  for (i = 0; i < 5; ++i)
    printf("%d\n", x[i]);
}

if i compile this with gcc i get a compiler error: 如果我用gcc编译这个我得到一个编译器错误:

foo.c:10: error: array subscript is not an integer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM