The pointer is a variable which holds the memory address of another variable. If one variable contains the address of another variable, the first variable is said to point to the second.

For a type T , T * is the type ‘”pointer to T.” That is, a variable of type T * can hold the address of an object of type T.

The C+ + Programming Language Third Edition by Bjarne Stroustrup

Declaring a Pointer

If a variable is holding the address of another variable then we can declare it as

Here var_type is the valid C++ Language data type and varName is the name of the pointer variable. The type of a variable defines that which variable type a pointer can hold.

There are two types of pointer operators; * and &. the & is a unary operator that returns the memory address of its operand. e.g.

vvarName = &newVar;

The * operator is the compliment of the & operator. It returns the value of the variable located at the address that follows. e.g.

The unary operator & gives the address of an object … The unary operator * is the indirection or dereferencing operator; when applied to a pointer, it accesses the object the pointer points to.

The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie

Pointers in C++

Pointers in C++

Pointer Example

Examine the program named POINTERS.CPP for a simple example of the use of pointers.

Output of the C Program

This is a pointer review and if you are comfortable with the use of pointers, you can skip this example program completely. A pointer in either ANSI-C or C++ is declared with an asterisk preceding the variable name. The pointer is then a pointer to a variable of that one specific type and should not be used with variables of other types. Thus pt_int is a pointer to an integer type variable and should not be used with any other type. Of course, an experienced C programmer knows that it is simple to coerce the pointer to be used with some other type by using a cast, but he must assume the responsibility for its correct usage.

If you are not completely comfortable with this trivial program using pointers, you should review the use of pointers in any good C programming book before proceeding on because we will assume that you have a thorough knowledge of pointers throughout the remainder of this tutorial. It is not possible to write a C program of any significant size or complexity without the use of pointers.

Constant Pointers and Pointers to Constants

The definition of C++ allows a pointer to a constant to be defined such that the value to which the pointer points cannot be changed but the pointer itself can be moved to another variable or constant. The method of defining a pointer to a constant is illustrated in line 22. In addition to a pointer to a constant, you can also declare a constant pointer, one that cannot be changed. Line 23 illustrates this. Note that neither of these pointers are used in illustrative code.

Either of these constructs can be used to provide additional compile time checking and improve the quality of your code. If you know a pointer will never be moved due to its nature, you should define it as a constant pointer. If you know that a value will not be changed, it can be defined as a constant and the compiler will tell you if you ever inadvertently attempt to change it.

A Pointer to void

The pointer to void is actually a part of the ANSI-C standard but is relatively new so it is commented upon here. A pointer to void can be assigned the value of any other pointer type. You will notice that the pointer to void named general is assigned an address of an int type in line 15 and the address of a float type in line 20 with no cast and no complaints from the compiler. This is a relatively new concept in C and C++. It allows a programmer to define a pointer that can be used to point to many different kinds of things to transfer information around within a program. A good example is the malloc() function which returns a pointer to void. This pointer can be assigned to point to any entity, thus transferring the returned pointer to the correct type.

A pointer to void is aligned in memory in such a way that it can be used with any of the simple predefined types available in C++, or in ANSI-C for that matter. They will also align with any compound types the user can define since compound types are composed of the simpler types.

Be sure to compile and execute this program.

Comments