Lesson 6
MB I MAK GAME NOW ?
Ok so we have covered loops conditionals, variables, and functions, we are going to concentrate on a new type of variable for this lesson, the pointer. Ok first thing though we have to get something about functions out of the way, every function has it's own set of variables and while the code for the functions is stored in memory when we first start the program(after all it doesn't change), the variables are not, they are only stored in memory when we call the function, to do this we have a special structure called "the stack" the stack pretty much works like a stack, so we can only access the top item, the last item placed there. The stack is used when we call functions, each time we call a function we add a layer to the stack filled with all the variables that are used in that function. When we are finished with the function, those variables go away never to be seen again, the only one that can be saved is the return value, and that is returned to the previous layer. When a function is called we consider it to be "in scope" this means that all it's variables can be used, the function that called our function is not in scope and therefore we can't use it's variables. This allows us to have the same variable name in our code, as long as they are in different functions. You may have noticed this with our calculator program already. The main advantage of this system is that we use as little memory as possible, it allows us to use consistent naming schemes, makes calling a function within a function simpler, and allows us to call the function that was called (recursive loops).
Now this doesn't really matter to you at this point, what does matter is you can't change any values outside of your function's scope, and that sucks. So enter the pointer. Pointers allow us to pass not the value inside the variable, but rather the location of the variable (remember variables are their address and their size) and so we know where to look even if it is outside our scope. So what we are talking about here is a type that stores the locations of variables, every type in C++ has an equivalent pointer even pointers have equivalent pointers(and so on)! Pointers are very similar to variables except their value is actually an address to a real variable. Pointers themselves are actually exactly the same, they all store a number, and unless you are using a 64 bit platform, they store their value in four boxes(bytes). Pointers are required to be typed, because unlike a variable that knows both the address and size of the data, pointers only store the address as their value, so we have to give the pointer a type so we know how many boxes are included in the address. Are you confused yet ? Another way to look at pointers is just what the name implies, that they are pointing to something. Alright we are going to return to pointers alot but lets get on with a bit of code showing how to create pointers and assign them:
Ok so I used two new operators there, the * and the & operator these are two operators used when working with pointers, the * operator is used to look at the address stored in the pointer, so it makes it like a normal variable. The & operator works in the opposite way it evaluates to the address of whatever is on the right of it. To note you will often find pointers declared in this manner as well:
int *an_int_pointer;
I personally prefer the * to the left because it is part of the type not an indirection operator or part of the name. Ok so now you can make a pointer out of any type, you just have to add a star to it, you can even make a pointer to a pointer type:
Now if you have run these you will have noticed the odd values printed for addresses, these are hex values, they are the same as normal numbers, at the end of this lesson I will give you a quick guide on how to read them. Hex values are used to specify memory addresses, you may remember them from HTML coloring if you are familiar with HTML. Alright that is about it for today, tomorrow we are going to take a look at Arrays, and expand on pointers. I know today was short, but you really have to understand pointers, it is absolutely vital, try doing things with multilevel pointers(pointers to pointers are 2 level pointers). If you like, you can try to do this concept visually by drawing variables as squares, and pointers with squares and an arrow pointing to a variable.
MB I MAK GAME NOW ?
Ok so we have covered loops conditionals, variables, and functions, we are going to concentrate on a new type of variable for this lesson, the pointer. Ok first thing though we have to get something about functions out of the way, every function has it's own set of variables and while the code for the functions is stored in memory when we first start the program(after all it doesn't change), the variables are not, they are only stored in memory when we call the function, to do this we have a special structure called "the stack" the stack pretty much works like a stack, so we can only access the top item, the last item placed there. The stack is used when we call functions, each time we call a function we add a layer to the stack filled with all the variables that are used in that function. When we are finished with the function, those variables go away never to be seen again, the only one that can be saved is the return value, and that is returned to the previous layer. When a function is called we consider it to be "in scope" this means that all it's variables can be used, the function that called our function is not in scope and therefore we can't use it's variables. This allows us to have the same variable name in our code, as long as they are in different functions. You may have noticed this with our calculator program already. The main advantage of this system is that we use as little memory as possible, it allows us to use consistent naming schemes, makes calling a function within a function simpler, and allows us to call the function that was called (recursive loops).
Now this doesn't really matter to you at this point, what does matter is you can't change any values outside of your function's scope, and that sucks. So enter the pointer. Pointers allow us to pass not the value inside the variable, but rather the location of the variable (remember variables are their address and their size) and so we know where to look even if it is outside our scope. So what we are talking about here is a type that stores the locations of variables, every type in C++ has an equivalent pointer even pointers have equivalent pointers(and so on)! Pointers are very similar to variables except their value is actually an address to a real variable. Pointers themselves are actually exactly the same, they all store a number, and unless you are using a 64 bit platform, they store their value in four boxes(bytes). Pointers are required to be typed, because unlike a variable that knows both the address and size of the data, pointers only store the address as their value, so we have to give the pointer a type so we know how many boxes are included in the address. Are you confused yet ? Another way to look at pointers is just what the name implies, that they are pointing to something. Alright we are going to return to pointers alot but lets get on with a bit of code showing how to create pointers and assign them:
PHP:
#include <iostream.h>
int main ()
{
int an_int;
int* an_int_pointer;
an_int = 7;
an_int_pointer = &an_int; // new operator here the address operator
// this evaluates to the address of an_int
cout << an_int;
cout << "\t";
cout << an_int_pointer; //just like ints you can print pointers but it prints the value
cout << "\n";
cout << *an_int_pointer; //Another new operator the dereference operator
//this operator works the oppisite way of the address
//operator and gives the value an_int_pointer is pointing at
return 0;
}
Ok so I used two new operators there, the * and the & operator these are two operators used when working with pointers, the * operator is used to look at the address stored in the pointer, so it makes it like a normal variable. The & operator works in the opposite way it evaluates to the address of whatever is on the right of it. To note you will often find pointers declared in this manner as well:
int *an_int_pointer;
I personally prefer the * to the left because it is part of the type not an indirection operator or part of the name. Ok so now you can make a pointer out of any type, you just have to add a star to it, you can even make a pointer to a pointer type:
PHP:
#include <iostream.h>
int main ()
{
int an_int;
int* an_int_pointer;
int** an_int_pointer_pointer;
an_int = 7;
an_int_pointer = &an_int;
an_int_pointer_pointer = &an_int_pointer;
cout << an_int;
cout << "\t";
cout << an_int_pointer;
cout << "\n";
cout << *an_int_pointer;
cout << "\t";
cout << an_int_pointer_pointer; //this prints the address of an_int_pointer
cout << "\n";
cout << *an_int_pointer_pointer;//prints the value of an_int_pointer
cout << "\t";
cout << **an_int_pointer_pointer; //prints the value an_int_pointer points to.
return 0;
}
Now if you have run these you will have noticed the odd values printed for addresses, these are hex values, they are the same as normal numbers, at the end of this lesson I will give you a quick guide on how to read them. Hex values are used to specify memory addresses, you may remember them from HTML coloring if you are familiar with HTML. Alright that is about it for today, tomorrow we are going to take a look at Arrays, and expand on pointers. I know today was short, but you really have to understand pointers, it is absolutely vital, try doing things with multilevel pointers(pointers to pointers are 2 level pointers). If you like, you can try to do this concept visually by drawing variables as squares, and pointers with squares and an arrow pointing to a variable.