Array: Difference between revisions
InvaderZim (talk | contribs) No edit summary |
Le Glaconus (talk | contribs) (formatting) |
||
(9 intermediate revisions by 9 users not shown) | |||
Line 1: | Line 1: | ||
{{languageBar}} | |||
'''Array'''s are a set of | '''Array'''s are a set of {{wiki|variable}}s of the same type with a built in capacity | ||
An example of an array is below: | An example of an array is below: | ||
<source lang=cpp> | |||
int lotsOfInts[100]; | |||
</source> | |||
This declares an [[integer]] '''array''', with at maximum 100 members. | This declares an [[integer]] '''array''', with at maximum 100 members. | ||
Line 12: | Line 14: | ||
Therefore | Therefore | ||
<source lang=cpp> | |||
lotsOfInts[3]; | |||
</source> | |||
actually refers to the 4th [[integer]] member of the '''array'''. | actually refers to the 4th [[integer]] member of the '''array'''. | ||
Strings can be created by making a [[char]] | Strings can be created by making a [[character|char]] '''array''', as seen below. | ||
<source lang=cpp> | |||
char thisIsAString[5]; | |||
thisIsAString[0]='H'; | |||
thisIsAString[1]='e'; | |||
thisIsAString[2]='l'; | |||
thisIsAString[3]='l'; | |||
thisIsAString[4]='o'; | |||
</source> | |||
Or this can be performed in one action as follows: | Or this can be performed in one action as follows: | ||
<source lang=cpp> | |||
char thisIsAString[5] = {'H','e','l','l','o'}; | |||
</source> | |||
The same style of setting up an array applies to any type of variable. | The same style of setting up an array applies to any type of variable. | ||
However strings are more commonly stored in a [[char]]*, a character [[pointer]] | However strings are more commonly stored in a [[char]]*, a character [[pointer]]. | ||
[[pointer]] | [[pointer|pointers]] can be used fairly similarly to '''arrays''' | ||
'''Array''' members can also be accessed via [[pointer]] | '''Array''' members can also be accessed via [[pointer|pointers]]. | ||
Custom classes can be used in '''arrays'''. | Custom classes can be used in '''arrays'''. | ||
[[Vector]]s are an alternative to '''arrays''' that allow more mathematical operations suited to manipulating various quantities related to geometry and physics, such as force [[vector]]s and normal [[vector]]s. | [[Vector]]s are an alternative to '''arrays''' that allow more mathematical operations suited to manipulating various quantities related to geometry and physics, such as force [[vector]]s and normal [[vector]]s. | ||
[[Category:variables]] |
Latest revision as of 10:05, 28 May 2025
Arrays are a set of variables of the same type with a built in capacity
An example of an array is below:
int lotsOfInts[100];
This declares an integer array, with at maximum 100 members. This is because when a compiler sees the 100 within the braces it knows to allocate enough memory to the array to hold 100 integers.
When accessing a member of an array it is important to remember that they are numbered starting at zero. Therefore
lotsOfInts[3];
actually refers to the 4th integer member of the array. Strings can be created by making a char array, as seen below.
char thisIsAString[5];
thisIsAString[0]='H';
thisIsAString[1]='e';
thisIsAString[2]='l';
thisIsAString[3]='l';
thisIsAString[4]='o';
Or this can be performed in one action as follows:
char thisIsAString[5] = {'H','e','l','l','o'};
The same style of setting up an array applies to any type of variable. However strings are more commonly stored in a char*, a character pointer. pointers can be used fairly similarly to arrays
Array members can also be accessed via pointers.
Custom classes can be used in arrays.
Vectors are an alternative to arrays that allow more mathematical operations suited to manipulating various quantities related to geometry and physics, such as force vectors and normal vectors.