C++ Programming/Operators/Pointers

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Contents

[edit] Pointers, Operator "*"

The "*" operator is used when declaring pointer types but it is also used to get the variable pointed to by a pointer.

Pointer a pointing variable b. Note that b stores number, whereas a stores address of b in memory (1462)

Pointers are important data types due to special characteristics. They may be used to indicate a variable without actually creating a variable of that type. They can be a difficult concept to understand, some special effort should be spent on understanding the power they give to programmers.

Pointers have a very descriptive name. Pointers variables only store memory addresses, usually the addresses of other variables. Essentially, they point to another variable memory location, a reserved location on the computer memory. You can use a pointer to pass the location of a variable to a function, this enables the function's pointer to use the variable space, so that it can retrieve or modify its data. You can even have pointers to pointers, and pointers to pointers to pointers and so on and so forth.

[edit] Declaring

Pointers are declared by adding a * before the variable name in the declaration, as in the following example:

int* x;  // pointer to int.
int * y; // pointer to int. (legal, but rarely used)
int *z;  // pointer to int.
int*i;   // pointer to int. (legal, but rarely used)

NOTE:
As always whitespace does not matter, so the position of the * doesn't matter only the order of the use.
Due to historical reasons some programmers refer to a specific use as:

// C codestyle
int *z;
 
// C++ codestyle
int* z;

As seen before check the coding style conventions used and adhere to a single use.

Watch out, though, because the * associates to the following declaration only:

int* i, j;  // CAUTION! i is pointer to int, j is int.
int *i, *j; // i and j are both pointer to int.

You can also have multiple pointers chained together, as in the following example:

int **i;  // Pointer to pointer to int.
int ***i; // Pointer to pointer to pointer to int (rarely used).
[edit] Assigning values
TODO

TODO
Missing info

[edit] Dereferencing

The "*" operator is used to get the variable pointed to by a pointer. It is also used when declaring pointer types.

TODO

TODO
Missing info

[edit] Null Pointer

The null pointer is a special status of pointers. It means that the pointer points to absolutely nothing. It is an error to attempt to dereference (using the * or -> operators) a null pointer. A null pointer can be referred to using the constant zero, as in the following example:

int i;
int *p;
 
p = 0; //Null pointer.
p = &i; //Not the null pointer.

Note that you can't assign a pointer to an integer, even if it's zero. It has to be the constant. The following code is an error:

int i = 0;
int *p = i; //Error: 0 only evaluates to null if it's a pointer

There is an old macro, defined in the standard library, derived from the C language that inconsistently has evolved into #define NULL ((void *)0), this makes NULL, always equal to a null pointer value (essentially, 0).

NOTE:
It is considered as good practice to avoid the use of macros and defines as much as possible. In the particular case at hand the NULL isn't type-safe. Any rational to use it for visibility of the use of a pointer can be addressed by the proper naming of the pointer variable.

Since a null pointer is 0, it will always compare to 0. Like an integer, if you use it in a true/false expression, it will return false if it is the null pointer, and true if it's anything else:

#include <iostream>
 
void IsNull (int * p)
{
  if (p)
    std::cout<<"Pointer is not NULL"<<std::endl;
  else
    std::cout<<"Pointer is NULL"<<std::endl;
}
 
int main()
{
  int * p;
  int i;
 
  p = NULL;
  IsNull(p);
  p = &i;
  IsNull(&i);
  IsNull(p);
  IsNull(NULL);
 
  return 0;
}

This program will output that the pointer is NULL, then that it isn't NULL twice, then again that it is.

TODO

TODO
Make short introduction to pointers as data members (so it can be cross linked from the function and class sections of the texts)

[edit] Pointers to Classes
[edit] Indirection Operator "->"

This pointer indirection operator is used to access a member of a class pointer.

[edit] Member Dereferencing Operator ".*"

This pointer-to-member dereferencing operator is used to access the variable associated with a specific class instance, given an appropriate pointer.

[edit] Member Indirection Operator "->*"

This pointer-to-member indirection operator is used to access the variable associated with a class instance pointed to by one pointer, given another pointer-to-member that's appropriate.

[edit] Pointers to functions

When used to point to functions, pointers can be exceptionally powerful. A call can be made to a function anywhere in the program, knowing only what kinds of parameters it takes. Pointers to functions are used several times in the standard library, and provide a powerful system for other libraries which need to adapt to any sort of user code. This case is examined more in depth in the Functions Section of this book.