Pointers

This will be my first technical post. I’m using this to revise for a test, but I’m sure the perspective of someone who’s new to this and is still learning will help others who’re just starting out.

Everything discussed in this post will be in the context of C++, the compiler being an old Turbo C++ 3.0 (released back in 1991-92). Don’t ask me why I’m using such an archaic compiler.

Pointers are simply variables that hold a memory location. There are two operators in C++ that are basic to doing anything with pointers: & and *.

& means “address of” and * means “at address”. Here, the word ‘address’ refers to memory addresses, which are usually represented in hexadecimal. Hexadecimal numbers are prefixed with 0x (zero x), so if I want to refer to a memory location of 1000 in hexadecimal, I’d write the corresponding memory address as 0x1000.

In programming terminology, & is used for referencing and * is used for dereferencing a pointer.

So in C++, you declare a pointer as follows:

int *p;

int means that the pointer p points to an integer. If you try to point it to a variable of any other data type, it’ll cause problems.

You should investigate the kinds of problems pointing a pointer of one datatype to a variable of another data type would cause only if you really want to become a complete expert in predicting outputs in dry runs. In all probability, the compiler would probably throw an error.

In the above declaration, p holds the memory address and *p refers to the value stored at that memory address. But there is one thing one should take care of, and that is initialisation.

Normally, there are two things you do with a pointer as far as assignment is concerned.

  1. Assign a memory location for the pointer to point to.
  2. Assign a value to the memory location already pointed to by the pointer (by assigning a memory location for the pointer to point to in a previous statement, in accordance with point 1).

Assignment is done as follows:

int x; //declaration of normal variable
int *p; //declaration of a pointer to an integer
p = &x; //assignment in accordance with point 1
*p = 5; //assignment in accordance with point 2, and equivalent to x=5 in every respect

The & operator gives the address of a variable. In the above example, &x gave the memory address of x. &p would give the memory address of the variable p, which in turn holds the memory address of x.

As I was saying, initialisation is only a bit difference. You would expect the following code to be an assignment of the value pointed to by the pointer, but instead, it is an assignment of a memory address to the pointer variables.

int x;
int *p = &x; //this is how to write lines 2 and 3 of the previous code block in one line

In essence, the only things to be remembered about pointers (the rest is all derivable) are the following three things:

int *p;
  1. *p, called dereferencing the pointer p, gives the value in the memory address stored in the variable p.
  2. Simply p gives the actual memory address stored in the variable p rather than the address pointed to by it.
  3. &x, called referencing the variable x, gives the memory address at which the value stored in the variable x is kept.

A simple question that arises is, “Why do we need to write int *p; at all? Why can’t we just write int p; and then do things like p = &x; cout << *p;?”

The probable reason is that the unary * operator only works with variables declared as pointers. A simple reason, really.

 
6
Kudos
 
6
Kudos

Now read this

Pencil

A very, very versatile tool. Writing is something that only humans can do. And the pencil is the perfect tool to exercise this ability. It’s better than pens because you can erase pencil marks and reuse the same space. Drawing is another... Continue →