Skip over navigation


Pointers and Basic Structures in C++




A pointer is a variable that represents a location in memory; since every variable that you declare has a space in memory, pointers can "point" to the location of any variable. You can find a variable's memory location with the & operator, which returns a variable's address (a constant):

int some_var = 3;
cout << &some_var;	// print out the address of the variable some_var

The output will be a hexadecimal number that will look something like "0x9e145450." You can create a pointer variable to store this location:

int* pointer_var;		// a pointer to an integer
pointer_var = &some_var;	// assign the address location to the pointer

You can then access both the address of some_var, and the value of some_var using the pointer_var variable:

cout << pointer_var << endl;	// prints out the address of some_var,
				// to which pointer_var is now pointing

cout << *pointer_var;		// prints out the value of some_var, 
				// which was defined to be 3 above.

The variable name preceded by an asterisk evaluates to the stored value, whereas the variable name alone evaluates to an address. It is important to recognize the difference between the value of a variable and the variable's location in memory, so it may be worthwhile to reread the above paragraphs; pointers are often considered one of the most difficult aspects of programming to learn. A review of pointer basics can be found here.

Passing by reference

Pointers are often passed to functions. This is called "passing by reference." When you simply pass a variable instead of a pointer to it, you are "passing by value." For instance, the following will not work as the programmer might intend because the variable is passed by value:

void triple(int value){
	value *= 3;	// The value is only changed inside the function

When the function finishes, the program forgets what it did to value. This is because the function created an entirely new variable for value, which goes out of scope when the function returns. You must instead pass by reference by adding a & to the end of the argument data type:

void triple(int& value){
	value *= 3;

Now when triple() finishes, the variable passed in has increased three-fold. The classic swap function could be defined as follows:

template <class T>
void swap(T& a, T& b){
	T temp = a;
	a = b;
	b = temp;

If a and b were instead passed by value, the swap function would not work as intended.


Structs have been made all but obsolete by the introduction of classes to C++, but they are worth mentioning as a way to encapsulate data. They are used to keep track of collections of variables which ought to go together for some reason. Consider a Cartesian plane. To specify a point on the plane, you need to give two values: an x coordinate and a y coordinate. You can create a structure type to keep track of points:

struct position{
	float xcoor;
	float ycoor;
} p1, p2;

position p3;

The above code creates three position types. The first two are created immediately after the struct definition, and the third is created separately. The position variables have no value at this point, but you can access their components with the . operator:

p1.xcoor = 5.0;
p1.ycoor = 7.3;

Sometimes you will have a pointer to a struct type, for example when creating a linked list. To access a data member of the struct pointer, you use the membership access operator ->:

position* pos_pointer = &pos;
pos_pointer->xcoor = 6.1;

You could also use the dot notation (*pos_pointer).xcoor, but this is uncommon and somewhat cumbersome, making it potentially confusing.

Follow Us