Array: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
(formatting)
 
(9 intermediate revisions by 9 users not shown)
Line 1: Line 1:
[[category:variables]]
{{languageBar}}
'''Array'''s are a set of [[variable]]s of the same type with a built in capacity
'''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:


    int lotsOfInts[100];
<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


lotsOfInts[3];
<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]]acter '''array''', as seen below.
Strings can be created by making a [[character|char]] '''array''', as seen below.
 
<source lang=cpp>
char thisIsAString[5];
char thisIsAString[5];
thisIsAString[0]='H';
thisIsAString[0]='H';
thisIsAString[1]='e';
thisIsAString[1]='e';
thisIsAString[2]='l';
thisIsAString[2]='l';
thisIsAString[3]='l';
thisIsAString[3]='l';
thisIsAString[4]='o';
thisIsAString[4]='o';
</source>


Or this can be performed in one action as follows:
Or this can be performed in one action as follows:


char thisIsAString[5] = {'H','e','l','l','o'};
<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]]s can be used fairly similarly to '''arrays'''  
[[pointer|pointers]] can be used fairly similarly to '''arrays'''  


'''Array''' members can also be accessed via [[pointer]]s.
'''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

English (en)Deutsch (de)Español (es)Translate (Translate)

Arrays are a set of Wikipedia icon 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.