You are here:

C++/hi

Question
hi can you explain to me in detail whats a pointer and array with examples and how they work?

Hello Raymond.

This is a big open question, so my answer may cause you to have more questions, but that's OK.

Arrays
========

You can think of an array as a collection of data. Say you have a program that uses 10 numbers. You could have ten integers called number1, number2, number3, and so on, or you could create an array called numbers that stores all 10. If your numbers are integers, you would declare the array like this:

int numbers[10];

This tells the computer to set aside space for 10 integers and you store your numbers in that space. With arrays, the first element is at location 0, so you would store your first number like this:

numbers[0] = something;

The last element of the array is always the size of the array minus 1, so you would store your last number like this:

numbers[9] = something;

In C and C++, you have to keep track of how large the array is. If you write past the end of the array like this:

numbers[99] = something;

then your program will probably fail in some unpredictable way at some unpredictable time. Other languages, like Java, will tell you right away if you've gone out of the array bounds.

An array can be of any type, not just integer. For floating point numbers you might have

float myArray[15];

which will set aside space for 15 floating point numbers.

Memory and Pointers
===================
The space for an array is set aside in the program memory. An integer is 4 bytes long so an array of 10 integers will be 40 bytes in the program memory. In C and C++, you can access memory locations directly through pointers. I will tell you how pointers and arrays are connected at then end.

A pointer to an integer is declared like this:

int* myPointer;

This says that myPointer is a pointer to an integer. The '*' makes it a pointer. The pointer is not yet set to point to anything, so it would be an error to try to use (or de-reference) the pointer. You can take the address of a variable in your program with the '&' operator.

int myInteger;
int* myPointer;
myPointer = & myInteger;

Now myPointer points to the same memory that myInteger is stored in. I read the '&' operator as "address of" so "myPointer equals address of myInteger."

We can check what is in myInteger through the pointer with '*', the de-reference operator. I read the '*' operator as "pointed at"

something = *myPointer;

Read this as "something gets what is pointed at by myPointer".

You can also store something in memory like this:

*myPointer = something;

Read this as "pointed at by myPointer gets something"

Pointers and Arrays
===================
Pointers and arrays are connected. If you have
int myArray[0];
int* myPointer;

Then you can get the starting address of the array with:
myPointer = myArray;
or
myPointer = &myArray[0]; /* NOTICE the & */

You can then access the entire element of the array with pointers.
*myPointer is myArray[0]
*(myPointer+1) is myArray[1]
and so on.

Notice that I said integers are 4 bytes long, but I said you can access the second element of the array (myArray[1]) by adding just 1 to the pointer. That's because adding 1 to a pointer automatically moves the pointer by the correct number of bytes for the thing its pointing to. That pointer arithmetic is usually a special topic in books.

Well, I hope that starts you off. There is no substitute for reading books and experimenting. You can get free books on the internet and you can get free compilers from Microsoft at http://www.microsoft.com/express/

Try some experiments, and if you don't understand some result, send me a note.

C++

Volunteer

Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.