Taylor's Blog

Atypical ramblings

Pointers: Round 2

Last Saturday I had my final exam for my CS161 course. I did okay, but I could have done better. I realize now that I really don’t have a handle on how pointers work still, so the goal of this post is to try and help myself understand them a little bit more.
I was watching some videos from The New Boston when I noticed that in the comments section some people were saying that the video had mistakes. It’s a bit disheartening to hear that the people trying to teach you pointers can’t even get it right. I’m going to try and digest what was in the video and what the commentators said. Here’s my synopsis:

Pass-by-reference uses references, not pointers. Pass-by-address uses pointers. Pass-by-reference and pass-by-address are not the same thing. It’s an important distinction because pass-by-reference is handled differently when calling the function.

A pass-by-reference function using references looks like this: passByReference(int &x);

While a pass-by-address function using pointers looks like this: passByAddress(int *x);

The important difference here is that passByReference is used (called) the same way as passByValue:

passByReference(betty)

passByValue(betty)

A passByAddress requires that you specify an address or a pointer as the argument. That is, you need to send a pointer/address:

int myVar = 5; 
int *myPointer = myVar; //Pointer points to address of myVar
passByPointer(myPointer)

or you need to put an ampersand (&) in front of your variable to denote it’s address rather than it’s value:

passByAddress(&myVar)

About the & operator

“&” is for the address of something. But when used when declaring a new variable, it becomes a different thing.

int a;

&a; // address of a

int &a = b; // the “&” now means you’re declaring a reference to “b”.

Of course, when declaring a refence, you must immediately assign it to something. References must always refer to something, they can’t refer to nothing.

So…

int &a; // not valid

int &a = b; // valid

To use it with function, you need to declare the function this way, to accept references:

void function(int &a); // takes a reference to an int

And then you just send any “int” to it:

int a;
function(a);

It looks like we’re sending the value of “a”, but because the function was declared to accept references, not values, you’re actually sending a reference to it.

——

I also found some help from this StackOverflow thread:

void makePointerEqualSomething(int *pInteger)
{
 *pInteger = 7;
}

In the function declaration, * means you are passing a pointer, but in its actual code body * means you are accessing what the pointer is pointing at.

In the function declaration, * means you are passing a pointer, but in its actual code body * means you are accessing what the pointer is pointing at.

& means get the address of something, its exact location in the computers memory, so int &myVariable; in a declaration means the address of an integer or a pointer, while

int *pInteger;
int someData;
pInteger = &someData;

means make the pInteger pointer equal to the address of ‘someData’ – so now pInteger points at someData and can be used to access it when you deference it:

*pInteger = 9000;

You can read that last line as “what is at the memory address pInteger points to is equal to 9000.”

Updated: November 23, 2015 — 11:09 am

Leave a Reply

Your email address will not be published. Required fields are marked *

Taylor's Blog © 2015