Imagine you declare a variable:
Imagine inside your computer there is a warehouse called 'myVar'. That warehouse has contents, and it has an address. Right now, you don't care about the address. You have however put contents inside the warehouse: the value '5'.
We will also say that the address of this warehouse is '9514'. Every time you run the program, the address will be different. You do not need to concern yourself with the exact value of the address for now.
Let's say, though, that you do care about what the address to myVar is. Let's say you want to store the ADDRESS of myVar in another warehouse, 'theVar'. However, we use a special type of warehouse designed to store addresses of other warehouses.
The above code does just that. The * in the variable declaration indicates that theVar is intended to store the ADDRESS of another variable- it 'points' to another variable. Likewise, the & tells the computer that you don't want to store the CONTENTS of myVar, you want to store the ADDRESS of myVar. Now the value of theVar is '9514'. (theVar also has its own address, but do not worry about that.)
However, we might not care about the value of a warehouse like theVar. We might actually care about the value of the warehouse whose address is being stored in theVar. So we use the following:
What this does is create a new variable newVar. It then populates the variable. It goes to the warehouse theVar and retrieves its contents (9514). However, because of the * operator, it knows not to put that value 9514 into newVar. Instead, it goes to the warehouse whose ADDRESS is 9514, in this case myVar, retrieves myVar's contents (5), and then sticks THOSE in newVar.
=======
Let's declare another variable.
Now imagine you have a function:
Code:
int myFirstFunction(int inputVar) {
return inputVar + 5;
}
In your main function, you make the following call:
Code:
anotherVar = myFirstFunction(myVar);
Imagine myFirstFunction is a company. What you are conceptually doing is saying, "myFirstFunction, I want you to fill one of my warehouses, named 'anotherVar', with some contents. I know you need some input to work with, so you should go make a copy the contents of whatever's in the warehouse myVar, store it locally in one of your own warehouses, and work with it there."
Now, say I have another function:
Code:
void mySecondFunction(int * inputVar) {
*inputVar = *inputvar + 5;
}
and another call:
Code:
mySecondFunction(&myVar);
anotherVar = myVar;
mySecondFunction is another company. What you are saying is, "I'm giving you the ADDRESS of my warehouse named myVar. This is great because you can actually go to that warehouse and change its contents if you need to."
Inside the company (inside mySecondFunction), the company is expecting an ADDRESS to a warehouse, not the contents of a warehouse.
Once we're inside, mySecondFunction wants to go to the warehouse, take its contents, add 5 to them, and put them back into the warehouse. Which is what it does, and so myFirstFunction and mySecondFunction end up doing the same thing (after we stick the value of myVar into anotherVar, of course).
NOW: What happens if we did this? (Note no ampersand.)
Code:
mySecondFunction(myVar);
anotherVar = myVar;
What the company mySecondFunction is expecting is an ADDRESS. However, you are giving it the CONTENTS of the warehouse myVar. What mySecondFunction will do is try to use myVar's contents (in this case, 5) as an address to a warehouse, go to that warehouse, and mess with its contents. Maybe the warehouse at address 5 just happens to be myVar. In all likelihood, it will not.
That's obviously a bad thing. You don't know what warehouse is at address 5, or whether there's a warehouse there at all. But mySecondFunction barrels ahead blissfully ignorant of your true intentions, and might end up breaking your program.
NOW: What happens if we did this? (note the lack of *)
Code:
void mySecondFunction(int * inputVar) {
inputVar = inputvar + 5;
}
What this function would do would be to expect an address to a warehouse. Inside the function, however, the function would take the ADDRESS and change it by adding five* to it. It would not actually go to the warehouse at the address and do anything to its contents, and would not do much of anything at all.
* There are some subtle intricacies to doing pointer arithmetic that aren't covered right now.
What if the function read like this?
Code:
void mySecondFunction(int * inputVar) {
inputVar = inputvar + 5;
*inputVar = 10;
}
Again, the function would take the ADDRESS that it gets, increment it by five. But now it would go to this new address and replace its contents with '10'. Again, this is generally a bad thing. You don't know what warehouse, if any, lives 5 warehouses down the street from the warehouse at the address that you're originally given.
However, when you get to arrays, imagine arrays as a bunch of warehouses located one right after the other, down the street. Now being able to change the address itself and then work with the new address becomes useful.