Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
TobyAsE28116yNo, Not in an classical array. The index describes the offset into the data structure and since there are no fractional bits, this is impossible.
If your array isn't an array but rather a map, this would be possible. But that would be a totally different data structure. I believe a language like python sees the difference automatically, so you could just use an "array" with arbitrary indexes (strings, floats, objects, whatever).
Another Problem that emerges is that floating point numbers are not countable. How many elements does your array have if the largest index is two? How many numbers are there between 0 and 2? Infinitely many and that's definitely a problem if you want to loop over the "array". -
ddephor44466ySure, if you want to get the higher nibble of the first byte and the lower nibble of the second byte, you can access them by
unsigned char floatTestArray[2] = { 0xA0, 0x02 };
unsigned char wtf = floatTestArray[0.5];
printf( "WTF = %u", wtf );
==> WTF = 42
Try it with [0.75] or [0.18538573]
π -
saas13016yThe teacher said the subscript can be a floating point and i argued that it cannot be. He said you'll learn it laterπ€¦βοΈ.
Later like after I'm dead π. -
@kayb01000010 you can cast a float to a different type (an unsigned integer) and use that (will be almost completely garbage though). You'll want to use something from the standard library to grab a rounded off integer version of the float.
-
No. Indexes of an array indicate it's elements' placement in memory. Allocation starts at the base address, index 0, and increments by the size of the data type of the array.
For example, an int array has size of 4 bytes(typically) for each element. The locations would be 0x00, 0x04, 0x08, 0x0c, and so on. So using a floating point number for an index wouldn't make sense. What memory address would x[1.72962] occupy?
Related Rants
-
xjose97x19Just saw a variable in C named like this: long time_ago; //in a galaxy far away I laughed no stop.
-
elgringo41Student - Teacher renaming .c to .exe make the program executable ? Teacher - Yes A group of people stand up...
-
Cyborg15A guy and a girl are in a Java seminar. Afterward, the guy approaches the girl and asks, "Hey gurrl, can I ge...
Can the subscript of an array be a floating point number ?
i know, i know, i can find answers on google and i found one on quora but i want you guys to help me π€ in the comments
question
java
c++
c
array