Pointers and Arrays - a beginners guide
Views: 3889
Comments: 0
Posted: 10-16-2005
based on 1 votes.
Faves: 1 | + Faves
Page(s): 1 2 3 4 5 6 Next Page
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

Page(s): 1 2 3 4 5 6 Next Page
l3lueMage - http://www.micohono.com
This author is too busy writing tutorials instead of writing a personal profile!
Close