Académique Documents
Professionnel Documents
Culture Documents
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.
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;
Using Pointers
In order to use pointers, two new operators are required.
& *
Consider:
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
We can refer to the variable a pointer points to using the dereference operator:
*tokyo = 10;
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%...
Pointers know about the size of the type they point to and the quantum for addition is sizeof (type) .
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:
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;
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%...
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%...
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:
// declare array
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.
9 of 9
03/12/2007 12:17