Pointers Again

Pinky: I can't believe we are spending another day on pointers. What were we doing all yesterday, huh?

Brain: Yesterday we were learning how a pointer works. Today we are going to learn what we can do with one.

Pinky: Sure, whatever.

Brain: Let's look at the first program.

parray.txt

Brain: In this program we delcare an array of 200 elements. We fill each one of these elements with some data. Then they are outputted. On lines 34-40 we see that the array name holds the same address as the first element in the array and the second element is the address in the name of the array plus one.

Pinky: Umm, Brain, I just have one question. If you are adding one to the address then how come largeArray and (largeArray + 1) differ by 2?

Brain: The computer does some of the work for us. There is a little rule about memory, that the location address where a variable is stored must be divisible by its type size. So for integers which take up two bytes the address must be divisible by 2, and for floats which take up 4 bytes the address must be divisible by 4. So since the computer knows that the pointer is an integer and can only be divisible by two, it knows to add two to the address instead of one when you add to it.

Pinky: I guess that makes sense.

Brain: Then we "send" the array to the function to get manipulated. We don't actually send the whole array, because that would take long and it would also use up extra memory. Instead, we just send the address of the first element and the size of the array.

Pinky: These pointers can confuse me sometimes. I might forget to dereference them or something. Isn't there a better way to do this?

Brain: As a matter of fact, there is. They are called references. They are similar to pointers only they are kind of backwards.

Pinky: How are they backwards?

Brain: Well, you know how when you assign a pointer you leave off the '*', but any time you want to get the value you have to put the star '*' back on?

Pinky: Yes.

Brain: Well, references work the other way. To assign it a place to point you put the symbol "&" in front of it, but you don't need it when you are accessing the value. It is almost like creating a synonym for the variable name. These can also talk back and forth through functions. They don't, however, work as well with strings, and they can only be assigned when they are declared.

ref.txt

Brain: In this program we delcare three variables and three references to point to the variables. Since the references must point to somewhere when they are declared, the variables must be declared above the references so we can use them. When we access the different parts of the variable, like its address and its value, the reference and the actual variable are the same. We can also send the variable down to a function and have it manipulated through references. Then when we get back to the function we can use the local references to access the variable again.

Pinky: Okay. Now I'm really getting tired of all this referencing and indirection and dereferencing. Can't we learn something else for a little bit?

Brain: That's a good idea. We still have about one more day with arrays and pointers, though.

Pinky: That's just horrible.

[Home] [Back] [Next]