Learntofish's Blog

A blog about math, physics and computer science

Tutorial on Pointers in C++

Posted by Ed on July 11, 2009

In C++ there is something called pointers, and I’d like to explain what they are. But before I explain them I’d like to thank simplcool for his excellent tutorial on pointers in C++. Watch his videos: part1 and part2. Another introduction to pointers in C can be found here. (The picture on the left is from xkcd)

1) We first declare an integer variable x and assign the value 42 to it:

int x = 42;
cout << "The value of x is: " << x << endl;

The console output is: The value of x is 42

Now, we can ask for the memory adress of our variable x, i.e. where in the memory it is saved. We do this by using the reference  address operator &:

int x = 42;
cout << "The value of x is " << x << endl;
cout << "The memory adress of x is " << &x << endl;

The console output is:
The value of x is 42
The memory adress of x is 0x22ff74
Thus, to get the adress of the variable x we write &x.

2) What kind of data type do we need to store the adress? The answer is pointers. Pointers are declared by putting a * behind the data type:

int* ptr;   //declare the pointer ptr
ptr = &x;   //save the address of x in ptr
cout << "The pointer ptr points to the address of x which is " << ptr << endl;

The console output is:
The pointer ptr points to the address of x which is 0x22ff74.

3) We’ve stored the address of the variable x in our pointer ptr. But we can do more. We can ask: What is the content of the address at which ptr points? To gain access to the content we write *ptr:

cout << "The content of the address 0x22ff74 is " << *ptr << endl;

The console output is:
The content of the address 0x22ff74 is 42

4) We can change the value of x by using the pointer ptr:

*ptr = 17;     //ptr points at the address of x. Change the content of x to 17
cout << "The new content of x is " << endl;

Exercises:
1) Declare two integer variables c and d and assign the values c=5 and d=9. What is the memory address of c and d?

2) Define two pointers ptr1 and ptr2. Store the address of c in ptr1 and that of d in ptr2.

3) What is the content of the address at which ptr2 points? Use the * sign.

4) Change the value of c to 2050 by using ptr1.

Answers:
1)

int c=5;
int d=9;

cout << "The memory address of c is " << &c << endl;
cout << "The memory address of d is " << &d << endl;

The console output is:
The memory address of c is 0x22ff6c
The memory address of d is 0x22ff68
Note that the addresses may be different on your computer.

2)

int* ptr1;     //declare pointer 1
int* ptr2;     //declare pointer 2

ptr1 = &c;     //store address of c in ptr1
ptr2 = &d;     //store address of d in ptr2

3)

cout << "The content of the address at which ptr2 points is " << *ptr2 <<endl;

The console output is:
The content of the address at which ptr2 points is 9

4)

*ptr1 = 2050;
cout << "The new value of c is " << c << endl;

The console output is:
The new value of c is 2050

Thanks to Greg Houston for his source code to HTML converter.

Update (October 11, 2010): Binky Pointer Fun Video
Here’s a 3 minute video from the Stanford CS Education Library

.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: