Search code examples
arrayspseudocode

Do arrays and array size declarators need to be the same data type?


I'm studying basic programming logic and design, and an example question uses pseudocode declaring an integer-type size declarator in a real-type array. Am I wrong for thinking this to be incorrect due to different data types, or can a real-type array use an integer-type constant as a size declarator? Thank you in advance for any insight.


Solution

  • Array size is an integer. An array of objects can't have 11.25732 elements or "kitten" elements except maybe in Prolog :)

    The exact type of integer depends on the language, for example an unsigned 32-bit.

    Generally an array of any type can be accessed using [ix] where ix is an integer of some type, such as size_t for c++.

    In some languages arrays are key => value pairs where animals["kitten"] does work, but the size or count is still integer. Then arrays might be sparse, where [1] and [100] hold values but [2] - [99] are currently undefined.