简体   繁体   中英

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.

This link: Float Values as an index in an Array in C++

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.

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

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.

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. There hast to be more going on here.

There are no float s here. size is an integer, and 16 is an integer, and consequently size/16 is an integer as well.

Integer division rounds towards zero, so if size is in [0,16) , then size/16 == 0 . If size is in [16,32) , then size/16 == 1 , and so on. And if size is in (-16, 0] , then size / 16 == 0 as well.

( [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 )

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); 

So ar[3.4] is the same as saying

*(ar + 3.4)    (1)

From the C++ Standard section 5.7.1 - Additive operators we read that :

(...) 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 .

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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