Vous êtes sur la page 1sur 9

Introduction to Pointers

http://64.233.183.104/search?q=cache:MqkI_UzeZpcJ:hepwww.ph.qmul.ac.uk/mcps/pointers.html%...

This is G o o g l e's cache of http://hepwww.ph.qmul.ac.uk/mcps/pointers.html?desc=Introduction+to+pointers&file=pointers.html as retrieved on 8 Jul 2007 13:04:41 GMT. G o o g l e's cache is the snapshot that we took of the page as we crawled the web. The page may have changed since that time. Click here for the current page without highlighting. This cached page may reference images which are no longer available. Click here for the cached text only. To link to or bookmark this page, use the following url:
Google is neither affiliated with the authors of this page nor responsible for its content.

http://www.google.com/search?q=cache:MqkI_UzeZpcJ:hepwww.ph.qmul.ac.uk/mcps/pointers.html%3Fdesc%3DIntroduction%2Bto%2Bpointers%26file%3Dpointers.html+al

These search terms have been highlighted: alex martin introduction pointers

Home QMUL Home Physics Home SIM Homepage Lecture Notes Lecture 1 Lecture 2 Lecture 3 Lecture 4 Lecture 5 Lecture 6 Lecture 7 Lecture 8 Lecture 9 Appendices Books

Introduction to Pointers

The basic idea behind pointers is fairly simple. Consider the statement:

int i=8;

This declares an int variable, which must be stored at some address in memory. The situation will look something like this: Address Contents 0X10000000 0 0X10000001 0 0X10000002 0 0X10000003 8

1 of 9

03/12/2007 12:17

Introduction to Pointers

http://64.233.183.104/search?q=cache:MqkI_UzeZpcJ:hepwww.ph.qmul.ac.uk/mcps/pointers.html%...

Exercises Exams

There are 4 memory locations (bytes), because an int requires 4 bytes of storage. Here the memory locations (addresses) are labelled using hexadecimal notation. int variable is allocated 4 bytes starting at the address 0x10000000 (the first 3 holds the value 0 and the last holds 8). The actual address is generally allocated by the compiler/operating system, and its value is (usually) not important. A pointer is a special variable which can hold the value of an address. Pointers allow one to lookup and manipulate addresses of variables (objects). So in the example, a pointer to the variable i would hold the value 0x10000000.

Different types of Pointers


In C++ there are different types of pointers, depending on the type of variable (object) the pointer refers to. (This is because the different types occupy different amounts of memory). The declarations:

int* p; double* q;

Declares p to be of type pointer-to-int and q to be of type pointer-to-double N.B. The * symbol is really associated with the variable name rather than the type. Therefore if one wishes to declare two pointers one needs to write e.g.:

int *p,*q;

2 of 9

03/12/2007 12:17

Introduction to Pointers

http://64.233.183.104/search?q=cache:MqkI_UzeZpcJ:hepwww.ph.qmul.ac.uk/mcps/pointers.html%...

NOT

int* p,q;

The latter declares q to be an int, NOT a pointer-to-int.

Using Pointers
In order to use pointers, two new operators are required.

& *
Consider:

Take Address of Dereference

This can be setup using the following code:

3 of 9

03/12/2007 12:17

Introduction to Pointers

http://64.233.183.104/search?q=cache:MqkI_UzeZpcJ:hepwww.ph.qmul.ac.uk/mcps/pointers.html%...

int paris=5; int* tokyo = &paris; // create a pointer which holds the // address of i

The situation will now look like this:

We can refer to the variable a pointer points to using the dereference operator:

*tokyo = 10;

// assign 10 to the variable pointed-to by // tokyo i.e. paris

The example program pointer.cpp further illustrates pointers.

Pointer Arithmetic
Pointers are just (special) types of integer variables, which can be manipulated using e.g. addition/subtraction. So one can write, for example

int i[2];

4 of 9

03/12/2007 12:17

Introduction to Pointers

http://64.233.183.104/search?q=cache:MqkI_UzeZpcJ:hepwww.ph.qmul.ac.uk/mcps/pointers.html%...

int* p = &i[0]; p++;

// p points at the first element // p now points at the second element

Pointers know about the size of the type they point to and the quantum for addition is sizeof (type) .

Dangers with Pointers


Pointers are an extremely powerful, but by using them one can easily introduce hard-to-find bugs into one's code. In particular, because one can easily assign an arbitrary value to a pointer, it is easy to attempt to access some arbitrary address in memory. Consider the following code:

int* p = 0x8000; *p = 10;

This tries to store the value 10 at address 0x8000. Unless you can be sure that this address is free for use, this code will most likely result in the program crashing. As a second example, consider the return value from a function:

int* add2( int a, int b ) { int tmp = a+b; return &tmp; }

5 of 9

03/12/2007 12:17

Introduction to Pointers

http://64.233.183.104/search?q=cache:MqkI_UzeZpcJ:hepwww.ph.qmul.ac.uk/mcps/pointers.html%...

This is valid code....but using such a function is likely to cause (hard to spot) errors, since tmp is a temporary variable, whose scope is limited to the function and its use outside its scope e.g. via a dereferenced pointer is not defined... it will most likely lead to a program crash.

Pointers vs References
In many cases one can use pointers instead of references. However, wherever possible I would recommend to use of references. With pointers it is very easy to make mistakes. The advantage of pointers is that they are more flexible. ...They can be reassigned and set to 0. They are also needed to manage the free store Use of the const declaration can make pointers safer:

const int* p;

Declares a pointer to an integer constant. While,

int i; int* const q = &i;

declares a constant pointer (properties like a reference).

Using pointers as function arguments


Pointers are the traditional 'C' method of providing functions with arguments that can be modified. This demonstrated by the swapp.cpp example. Notice that the syntax is more complicated than using references and the functions with a pointer argument

6 of 9

03/12/2007 12:17

Introduction to Pointers

http://64.233.183.104/search?q=cache:MqkI_UzeZpcJ:hepwww.ph.qmul.ac.uk/mcps/pointers.html%...

should be called with the address of a variable. e.g. swap(&x, &y).

Relationship between C++ arrays and pointers


In C++ there is a close relationship between pointers and arrays. If, for example, one declares an array int a[10]; then a is automatically an int* pointer which holds the address of the first element of the array i.e. &a[0] Therefore an alternative method of accessing array elements is via a dereferenced pointer. *(a+4) is equivalent to a[4]. This equivalence between arrays and pointers in further demonstrated in the example array.cpp

Arrays as Function Arguments


Understanding the equivalence between arrays and pointers is essential to understanding how arrays can be passed as function arguments. If one wants to declare a function which accesses, for example, an int array then the function prototype can be declared to have an argument using the []. For example

void afun(int a[] ) { a[3] = 10; // set 4th element of the array to be 10 cout << a[3]; // print it }

Here the argument is declared to be an int array of unknown length. Inside the function, the array elements are accessed in the usual way. However, an alternative, and more common syntax is to declare the argument to be an appropriate pointer. i.e.

7 of 9

03/12/2007 12:17

Introduction to Pointers

http://64.233.183.104/search?q=cache:MqkI_UzeZpcJ:hepwww.ph.qmul.ac.uk/mcps/pointers.html%...

void afun(int* a) { *(a+3) = 10; cout << a[3]; }

// set 4th element of the array to be 10 // print it

Here, inside the function, the array elements can be accessed by either dereferencing the pointer, or using the [] syntax as usual. To use either of the functions, one would need to do something like:

int b[10]; ... ... afun(b);

// declare array

// Use the function

Notice that in this case there is no bounds checking on the array... the array passed as an argument could have dimension less than 4, in which case the program will behave in an undefined way, e.g. crash. Because of this it is generally a good idea to have the dimension of the array as an extra argument which can be used as protection. e.g.

void afun(int* a,int n) { if( n >= 4 ) { *(a+3) = 10; // set 4th element to be 10

8 of 9

03/12/2007 12:17

Introduction to Pointers

http://64.233.183.104/search?q=cache:MqkI_UzeZpcJ:hepwww.ph.qmul.ac.uk/mcps/pointers.html%...

cout << a[3]; // print it } else { cout << " Array is too small"; } }

// error

This is demonstrated in the example: arrayarg.cpp which uses a function that prints an array.

Dr. Alex Martin

Last updated on Mon Feb 5 21:29:55 2007

9 of 9

03/12/2007 12:17

Vous aimerez peut-être aussi