Anonymous user
Retrieving an Element of an Array: Difference between revisions
m
→{{header|Delphi}}/{{header|Object Pascal}}/Standard {{header|Pascal}}: lang tags
m (→{{header|C++}}: lang tag) |
|||
Line 170:
A Static array definition:
The base index can be freely chosen:
Indeed, the "1 .. 10" resp. "7 .. 16" are actually ''types'': they are integer subrange types. Arrays can also be indexed by enumeration types or enumeration subrange types:
rainbowcolor = (red, orange, yellow, green, blue, violet);
var
Line 183:
i := foo[red]; { allowed indices are red, orange, yellow, green, blue, violet }
i := bar[green]; { allowed indices are yellow, green, blue }
end;</lang>
A Dynamic Array type in Delphi:
An "old school" dynamic array in the various flavors of pascal
A dynamic array in Extended Pascal:
intarray(n: integer) = array[1 .. n] of integer; { base index 1 }
var
Line 197:
i := foo[2];
dispose(foo); { get rid of the array }
end;</lang>
In the case of the static array, the compiler generates the code to allocate the required memory to hold 10 integers.
In the Delphi style ---dynamic--- array you must set its length:
In the "old school" style of dynamic arrays, you created a point to the zero length declaration and then allocated memory to it with GetMem
Foo : array[0..0] of integer ;</lang>
All arrays are accessed the same way regardless of declaration method.
i := foo[n] ;</lang>
where n is the array index who's base is either 1 or 0 depending on how it was declared.
|