Q: Okay what are pointers, and why do we need them?
A: Pointers are the same as variables but instead of directly working with the variable, you are working with memory addresses.
Every variable in an executing program is given a section of memory large enough to hold a value of that variable's type. Each byte (or unit) of memory is given a unique address. The address given to any variable is the first byte of its allocation.
So if I allocate 4 units of memory, the address is the first byte in the sequence. So if I create an int variable called "myint", my compiler reserves 4 bytes of memory in a spot, then gives the address to the variable "myint". By saying &myint, I can receive and see the address. A value that represents the address of a memory location, or holds the address of a variable is called a pointer.
Now like any data type, memory address can be stored in what we call a pointer variable, or just simply a pointer. Every time you create a pointer variable it needs to know what type of memory its pointing to. (This is important for many things explained later on.) So if you want a pointer to hold the address of an int you must declare the variable as the type "int *variablename". The * (known as an asterisk) indicates that variablename is a pointer to an integer.
Q: Now why do we need pointers?
A: Pointers are really the underbelly of many functions in C and C++ and without them certain data would no longer be available. The main one is arrays. Arrays are the very heart are pointers. When you declare an array:
int array[10];
You are actually creating a constant pointer to the first element of the array.
Run this in C++:
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <iostream>
using namespace std;
int main()
{
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
cout << array << endl; // This should print out some Hex number (starting with 0x)
return 0;
}
| |
Note that cout << array; prints some random looking garbage. That is an address, and that means array[] is a pointer variable. But what is the point? Well arrays don't function without an important things from pointers: Dereferencing and Pointer-arithmetic.
Note how we access an item from an array. We usually call array[index]. What is that actually doing? Its a simplified syntax for pointers. What [] actually does in this case is that it does math on the pointer, incrementing its size by the index (like say 1) and the number of bytes that array is declared (in my case, its an int array, and my compiler says its 4 byte large). So first [] takes the pointer address which for now lets just say the address of my pointer is 20. So beyond 20 there's address 24, 28, 32, 36, and 40 which are the front most byte of the integers in my array. Now when I call array[1] I actually tell the compiler to do this.:
Take the address stored at array (in this case 20), and add 1 multiplied by the size of integers which is 4. Then tell me what value stored there. This in arithmetic looks like
, the address of the first integer memory location after 20! Now [] just translate the bits of data there and represents it as an int.
Q: But when will I need pointers?
A: In the long run, you'll need them when working with dynamic memory otherwise known as the HEAP. Programs are only allowed to use the HEAP through address, and pointers are the holders of addresses of memory. You see that funny relation?
Q: Do I need a pointer to work with arrays?
A: No. The [] does it for you.
Q: What if I want make the array larger or smaller?
A: You can't without pointers and working in the HEAP. This is very risky and error prone. When working the the HEAP you are required to clean up your own messes. Failure to do so causes problems. And thats not good. There is no janitor, custodian, or garbage man to clean these messes. Only the programmer. If you fail to clean up too much you will run out of memory and eventually the computer your working on will crash?
There is a substitute. Vectors. Vectors are dynamic sizing arrays. They grow and shrink as you need and they play in the HEAP. When your done with them, like when your program ends, they know how to clean up after themselves before they leave. They are nice an tidy!
You'll find that vectors also have many advantages over arrays.
1. Vectors are better at handling your own datatypes when you create structures and classes.
2. Vectors are smart. So smart they remember their size for you. No longer do you need to remember you only have 5 spaces in those old arrays.
3. Vectors can be really big and grow for the ocassion! Now if you read in data you no longer have to limit to 10 items. You also never need to make more space than you need.
If you used an array and wanted to nable users to imput as much as they wanted into it, the code would be very long, and you'd need to work in the HEAP. By using Vectors, lose that fear of error programs!
Q: So do I need pointers? Will I ever use them?
A: Yes. But if you don't understand them you shouldn't fear or panic. They are a complicated item, but once you begin using them crrectly you'll ask how you ever got by without them before!