Array

From Valve Developer Community
Jump to: navigation, search
English (en)Deutsch (de)español (es)
... Icon-Important.png

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 character 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.