Publishing System Settings Logout Login Register
Pointers and Arrays - a beginners guide
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on October 16th, 2005
6138 views
C++ General
We will cover how to declare and use arrays, how to pass them to functions. We will also cover 2-dimensional arrays, and how to pass them to functions also.
The pointers section will cover, how to declare pointers, how to use them correctly(assigning values from other variables correctly), how they work hand in hand with arrays and other variables, and how to use them with functions correctly. At the end we will give a couple tips to remember when using pointers that will help prevent errors.

Arrays and Pointers are pretty easy once you get the concept of it. Many people find pointers confusing but really they arent that hard.

-Arrays-
Arrays are easily thought of as a row of information put into memory. Depending on the size we input depends on how much we can store into the array.
Each array is indexed at 0 and counts up from 0. This confuses alot of people but as long as you remember everything starts with 0 with arrays, then you will be fine.
int sampleArray[5] = {4, 7, 8, 9, 4};

Here we have declared an array of 5 and stored 5 different numbers into the array. To call each number you type the array name, so "sampleArray", plus the square brackets and the number inside the brackets that you want to call.
Array diagram
By this image we can see which slot each number is in, and what number to put in the brackets to call a certain number. Remember arrays are indexed at 0, so to print the 7, we will use this code
cout << sampleArray[1] << endl;

One good feature of arrays is they do not store each number randomly into memory, they store them one right after another, while if we take to seperate variables, they most likely will be in two random spots of memory. We can see this by this piece of code
#include <iostream>

using namespace std;


int main ()
{
int value1 = 5, value2 = 15;
int array[5] = {4, 7, 8, 9, 4};

cout << \"Address of value1: \" << &value1 << endl;
cout << \"Address of value2: \" << &value2 << endl;
cout << \"Address of array slot 0: \" << &array[0] << endl;
cout << \"Address of array slot 1: \" << &array[1] << endl;
cout << \"Address of array slot 2: \" << &array[2] << endl;
cout << \"Address of array slot 3: \" << &array[3] << endl;
cout << \"Address of array slot 4: \" << &array[4] << endl;
return 0;
}


By adding the & in front of the variable, it shows us what address it is stored in memory. Each computer will be different, and it most likely will be different each time you run it. Here is what I got.

Address of value1: 0012FED4
Address of value2: 0012FEC8
Address of array slot 0: 0012FEAC
Address of array slot 1: 0012FEB0
Address of array slot 2: 0012FEB4
Address of array slot 3: 0012FEB8
Address of array slot 4: 0012FEBC
Press any key to continue


If you do not know hex then here is a quick lesson, Hex goes from 0-F, 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F and usually only has 6 numbers in it, the 00 in front of each of the results on mine do not matter.
Now as you can see value1 and value2 are not near eachother, at the end one is D4, and the other is C8. So this means they are 12 bytes apart from eachother, this isnt a huge difference but it just shows that the computer just stores each value randomly. But this is different with arrays, arrays make each value go one after the other. An integer equals 4-bytes, so each should be 4-bytes apart from eachother. AC+4 = B0, so thats true for slot 0 and 1, B0+4 = B4, so thats true for slot 1-2, B4+4 = B8, so thats true for slot 2-3, and B8+4 = BC, so thats true for slot 3-4.
This shows that arrays are somewhat more memory efficient.

-Matrix Arrays-
Matrix arrays are just like normal arrays, except they are 2-dimensional and they are declared this way:
int matrixArray[4][6] = {1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24};


These are a little tricky, the first [] means means rows, and the second means columns, so for this one the diagram would be:
Matrix Array

Its just like a criss-cross table, to find out how to print a number just match up the outside numbers. So if we wanted to print number 17, we would do
cout << matrixArray[2][4] << endl;


So if we wanted to find that with a picture we would do something like this


-Passing arrays to functions-
If you are not familiar with functions, I suggest you skip this step to prevent more confusion, I will write a tutorial all about functions soon ;)

To pass an array to a function isnt hard. Its just like any other variable, but we need to pass the size also. So, to declare the function you use this method
void fnExample(int array[], int size)
{
return;
}

This is the only way to pass an array through to a function without using pointers. Here is an example code that has an array passed and printed out in the function.


#include <iostream>

using namespace std;

void function(int array[], int size);

int main ()
{
int array2[3] = {1, 4, 7};
function(array2, 3);
return 0;
}

void function(int array[], int size) {
cout << array[2] << endl;
return;
}


You should get the number 4 in your output. To pass Matrix Arrays is a little less efficient, you need to declare the second bracket in the function, which means only arrays with that number of columns will be passed correctly. Here is an example code using the same matrix code as above:
#include <iostream>

using namespace std;

void function(int array[][6], int size);

int main ()
{
int matrixArray[4][6] = {1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24};

function(matrixArray, 4);
return 0;
}

void function(int array[][6], int size) {
cout << array[3][5] << endl;
return;
}


As you can see the 6 will always be there, so the number of columns must always be the same, but the number of rows can change. For this you should get the number 24.
Arrays are pretty neat for adding and subtracting, because you can always say something like this
total = array[1] + array[2];


Where total is a integer or float, and array is an integer or float. So lets say array[1] = 3, and array[2] = 6, the variable total will now equal 9.

-Pointers-

Pointers work hand in hand with arrays, pointers make it easier to pass arrays to functions and manage some variables. Pointers point to the address of a variable in memory, so if you change the value of a variable the pointer will also be changed. You declare pointers by putting a * in front of the variable.

#include <iostream>

using namespace std;

int main ()
{
int value1 = 5, value2 = 15;
int *p1, *p2;
p1 = &value1;
p2 = &value2;
cout << p1 << endl;
cout << *p1 << endl;
cout << p2 << endl;
cout << *p2 << endl;

return 0;
}


This small piece of code declares two integer variables and two integer pointers. Remember pointers point to the address, and the & in front of a variable prints the address, same concept with declaring a variable as something, if you put the & in front it will make it equal the address, or in this case point to the address.
So p1 is pointing to the address of variable value1, and p2 is pointing to the address of variable value2.
Notice I printed them differently, if you do not print the pointer with the * in front of it, it will print the address and not the value, with the star it prints the value of the variable its pointing to.

0012FED4
5
0012FEC8
15
Press any key to continue


These results are what I got, the addresses may be different for you.
Remember pointers point to the address, so the value of that address can change without using the pointer, and the pointer will show a different value. For example of we do
value1++;

*p1 will now print out 6 if we print it again.

#include <iostream>

using namespace std;

int main ()
{
int value1 = 5, value2 = 15;
int *p1, *p2;
p1 = &value1;
p2 = &value2;
cout << p1 << endl;
cout << *p1 << endl;
cout << p2 << endl;
cout << *p2 << endl;
value1++;
cout << *p1 << endl;

return 0;
}


Results I got are:

0012FED4
5
0012FEC8
15
6
Press any key to continue


Pointers are usually not passed to functions, but arrays are often passed to a function as a pointer. Lets take this code for example
#include <iostream>

using namespace std;

void pointers(int *pt);

int main ()
{
int array[5] = {4, 7, 11, 9, 4};

cout << array[1] << endl;
pointers(array);
cout << array[1] << endl;
return 0;
}

void pointers(int *pt)
{
pt[1]++;
return;
}


Notice that the pointer is declared the same in functions, but if you look inside the function called pointers, it looks like an array. Thats because we passed an array so it acts just like an array, its a pointer and points to an array so we use the brackets to choose which element we want on the array. Pointers also can change the value of a variable as you can see. pt[1]++ is adding 1 to array[1] which makes 7 goto 8. So if you look at the output from this code, you will see that it is

7
8
Press any key to continue


But if you put
*pt[1]++;

you will get a compile error because you are trying to add one to the address. Pointers can be confusing because of this. Here are a couple quick tips
When printing the value a pointer points to, add a * in front of it.
When changing the value of something, DO NOT put the * in front of it
When wanting to print the address of a variable, DO NOT put the * in front of it

These will help you alot with pointers. Alot of times pointers will be the biggest pain while coding, but other times they will be the most helpful.
Premium Publisher
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
l3lueMage

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top