<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wiki.planetchili.net/index.php?action=history&amp;feed=atom&amp;title=Arrays</id>
		<title>Arrays - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.planetchili.net/index.php?action=history&amp;feed=atom&amp;title=Arrays"/>
		<link rel="alternate" type="text/html" href="https://wiki.planetchili.net/index.php?title=Arrays&amp;action=history"/>
		<updated>2026-05-10T16:45:25Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.26.3</generator>

	<entry>
		<id>https://wiki.planetchili.net/index.php?title=Arrays&amp;diff=1481&amp;oldid=prev</id>
		<title>Rudra: Created page with &quot;Arrays are a collection of elements usually of the same type grouped together and referenced using an index to a unique identifier.   If you think of variables as boxes in whi...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.planetchili.net/index.php?title=Arrays&amp;diff=1481&amp;oldid=prev"/>
				<updated>2018-09-01T09:55:07Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;Arrays are a collection of elements usually of the same type grouped together and referenced using an index to a unique identifier.   If you think of variables as boxes in whi...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Arrays are a collection of elements usually of the same type grouped together and referenced using an index to a unique identifier. &lt;br /&gt;
&lt;br /&gt;
If you think of variables as boxes in which you can keep stuff and label them &amp;lt;code&amp;gt;box0&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;box1&amp;lt;/code&amp;gt; ... arrays can be thought of as a large box with compartments in it, where the box can be called &amp;lt;code&amp;gt;box&amp;lt;/code&amp;gt; and each compartment is labeled &amp;lt;code&amp;gt;box[0]&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;box[1]&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;box[2]&amp;lt;/code&amp;gt; and so on.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Using Arrays ==&lt;br /&gt;
=== Syntax ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
// create a big box that stores integers with 10 compartments&lt;br /&gt;
int box[10];&lt;br /&gt;
&lt;br /&gt;
// Now to put values in these compartments all we have to do is&lt;br /&gt;
box[0] = 50;&lt;br /&gt;
box[1] = 89;&lt;br /&gt;
box[2] = 69;&lt;br /&gt;
box[3] = 50;&lt;br /&gt;
.&lt;br /&gt;
.&lt;br /&gt;
.&lt;br /&gt;
box[9] = 30;&lt;br /&gt;
&lt;br /&gt;
// NOTE - There are 10 comparments in the box, so we start with compartment 0 &lt;br /&gt;
//        and go all the way to compartment 9.&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice how the array declaration follows the format: &lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
// TypeOfBox NameOfBox[CompartmentsInBox]&lt;br /&gt;
	int       box         [10]&lt;br /&gt;
&lt;br /&gt;
// To get access to a compartment use use the syntax  &lt;br /&gt;
// NameOfBox[CompartmentNumber]&lt;br /&gt;
	box[0] // Gives you access to the first compartment in the box. &lt;br /&gt;
	box[1] // Gives access to the second compartment&lt;br /&gt;
   box[9] // Gives access to the last compartment&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Other ways to declare an array: &lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
// An array of 20 ints&lt;br /&gt;
int box[20];&lt;br /&gt;
&lt;br /&gt;
// An array of 5 ints where the first three values are initialised, the other two have default values&lt;br /&gt;
int box[5] = {50, -35, 69};&lt;br /&gt;
&lt;br /&gt;
// Create an array of ints and initialize all elements to zero&lt;br /&gt;
int box[10] = {}&lt;br /&gt;
&lt;br /&gt;
// Both of these statements define an array of 3 ints initializes them with the values provided. &lt;br /&gt;
int box[] = {50, 23, 40};&lt;br /&gt;
int box[]{50, 23, 40};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The size of the array should be known at compile time and the size cannot be changed throughout the lifetime of the program. This is because the space for your container needs to be known at compile time&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
// FAIL &lt;br /&gt;
int size = 10;&lt;br /&gt;
int box[size]; // Error: Here the variable value can be changed during the life of the program because it is not a constant&lt;br /&gt;
&lt;br /&gt;
// PASS&lt;br /&gt;
constexp int size = 10; //  When using constexr it is guaranteed that size won't be changed and it will be known at complie time.&lt;br /&gt;
int box[size];&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Accessing compartments that don't exist is a common mistake, in debug mode you'll get an error but bad things will happen in release mode. Especially be careful while accessing the last compartment of the array. &lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
box[20] = 69 // Gives an error as our box only has 10 compartments and we're trying to access the 21st compartment. &lt;br /&gt;
box[10] = 69 // Again gives an error as our box has 10 compartments starting at 0, and going upto 9&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== But why arrays ?  ===&lt;br /&gt;
Imagine you wanted to store the health of 10 enemies you could store them as enemyHealth0, enemyHealth1 and so on&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
// Store the health of 10 enemies &lt;br /&gt;
int enemyHealth0 = 44&lt;br /&gt;
int enemyHealth1 = 35&lt;br /&gt;
int enemyHealth2 = 45&lt;br /&gt;
.&lt;br /&gt;
.&lt;br /&gt;
.&lt;br /&gt;
int enemyHealth9 = 84&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
So far so good, now lets say you wanted to display the health of all 10 of your enemies&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
DisplayHealth(enemyHealth0);&lt;br /&gt;
DisplayHealth(enemyHealth1);&lt;br /&gt;
DisplayHealth(enemyHealth2);&lt;br /&gt;
.&lt;br /&gt;
.&lt;br /&gt;
.&lt;br /&gt;
DisplayHealth(enemyHealth9);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
As you can probably see that you need to type DisplayHealth() repeatedly , what if we could do the same thing but with a lot less code ? That's where arrays come in: &lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
// Declare an array of size 10 and set health variables to&lt;br /&gt;
int enemyHealth[10] = {44, 35, 45 ... 84};&lt;br /&gt;
&lt;br /&gt;
// use a for loop to diplay enemy health&lt;br /&gt;
for(int i = 0; i &amp;lt; 10; i++)&lt;br /&gt;
{&lt;br /&gt;
	DisplayHealth(enemyHealth[i]);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
We could do the same if we wanted to kill off all enemies, just go through an array of all the enemies and kill them. &lt;br /&gt;
Arrays work really well with loops, in the above example we only had 10 values, but imagine if we had 100 values we could just loop over all of them with the same code by just changing the condition to &amp;lt;code&amp;gt;i &amp;lt; 100 &amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== What's really going on here tho == &lt;br /&gt;
When you create an array e.g. &amp;lt;code&amp;gt;int box[10]&amp;lt;/code&amp;gt; your computer takes a portion of your memory and gives a name to that block i.e. &amp;lt;code&amp;gt;box&amp;lt;/code&amp;gt;  in our case. The size of the block is decided by the type of the array. In our case int, a single int is &amp;lt;code&amp;gt;4 bytes&amp;lt;/code&amp;gt;  in size [https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx MSDN Reference] and we need space to store 10 ints so your computer allocates &amp;lt;code&amp;gt;10 x 4 bytes&amp;lt;/code&amp;gt; of memory i.e. &amp;lt;code&amp;gt;40 bytes&amp;lt;/code&amp;gt; of memory where each int occupies 4 bytes one after the other. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now when you access the third compartment in your array &amp;lt;code&amp;gt;box&amp;lt;/code&amp;gt; and use the syntax &amp;lt;code&amp;gt;box[3]&amp;lt;/code&amp;gt;. Your CPU says I am going to go to the starting point of box , you want the 3rd compartment? Ok, so each compartment is &amp;lt;code&amp;gt;4bytes&amp;lt;/code&amp;gt; long (int) so to get to the third compartment I can take the starting point of block and move 3 times the size of each compartment, which in our case would be &amp;lt;code&amp;gt;3 * 4 = 12 bytes&amp;lt;/code&amp;gt; . Now we are at the starting point of the third block. And because the type of the array is an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt; (size 4 bytes), your CPU knows that the next 4 bytes in the memory block are the contents of compartment 3. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you're starting out with arrays, you don't need to know how arrays work under the hood; more gems will be revealed as you move on to the intermediate series. What's important is that you get a solid understanding of the concept of arrays as being big compartmentalized containers to store stuff that work really well with loops. &lt;br /&gt;
&lt;br /&gt;
== Planet Chili Tutorial Video == &lt;br /&gt;
* [[Beginner C++ Game Programming Tutorial 13]]&lt;br /&gt;
&lt;br /&gt;
== Other Tutorials == &lt;br /&gt;
* http://www.cplusplus.com/reference/array/array/&lt;br /&gt;
&lt;br /&gt;
* https://www.tutorialspoint.com/cplusplus/cpp_arrays.htm&lt;br /&gt;
&lt;br /&gt;
* https://www.programiz.com/cpp-programming/arrays&lt;br /&gt;
&lt;br /&gt;
* https://www.geeksforgeeks.org/arrays-in-c-language-set-1-introduction/&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
* [[Beginner_C++_Game_Programming_Series#Topics_Covered|List of Beginner C++ Topics]]&lt;/div&gt;</summary>
		<author><name>Rudra</name></author>	</entry>

	</feed>