Handout 10

Some Pointers on Pointers


Pointer Types

For any variable type T, a pointer type "pointer to T" may be formed. Pointer types are referred to as object pointers. A value of pointer type is the address of an object of type T.

Example

The * is used in variable declarations to indicate that a variable is a pointer variable:

int *ip;   //  a pointer to an object (variable) of type int 
char *cp;  // a pointer to an object (variable) of type char 
float *fp; // a pointer to an object (variable) to type float

The two most important operators used in conjunction with pointers are:

Example

In the following example, the pointer ip is assigned the address of variable i (&i). After that assignment, the expression *ip refers to the same object denoted by i:

int i, j, *ip, *xp, *jp;
ip = &i;   // set ip to the address of i
jp = &j;   // set jp to the address of j
i = 22;    // simple assignment, set i equal to 22
j = *ip;   // j now has the value 22 
*ip = 17;  // i now has the value 17
xp = ip;   // the pointer ip and xp both point to the variable i 
*jp = *ip; // the variable j has same value (17) as the variable i 

Other operations on pointer types include assignment, addition of integers, subtraction, relational and equality tests, logical AND and OR, addition and subtraction of integers, and conversions to and from integers and other pointer types.

The size of a pointer is implementation dependent and in some cases varies depending on the type of the object pointed to.

Null Pointers

C has a special "null pointer" value that explicitly points to no object or function. The null pointer may be written as the integer constant 0, or using the constant NULL. The null pointer (like the interger value 0) has the value of "false" in boolean expressions, so that tests for null can be abbreviated.

Example

The statement:

     if (ip) i = *ip;

is a common notation for:

     if(ip != NULL) i = *ip;

It is good programming practive to set all pointers to the value NULL when they are not designating a valid object or variable.

Passing by Value and Reference

We can use the * and & operators to control how parameters are passed to functions. The following example shows pass by reference and value implementations.

Example

1    #include <iostream.h>
2
3    // function prototypes
4    void one(int);      // pass by value
5    void two(int *);    // pass by reference
6    void three(int &);  // pass by reference
7
8    int main() {
9
10     int i = 0, *ip;
11
12     cout << "the value of i is " << i << endl;   // output is 0
13
14     one(i);
15
16     cout << "the value of i is " << i << endl;   // output is 0
17
18     ip = &i;
19     two(ip);
20
21     cout << "the value of i is " << i << endl;   // output is 2
22
23     three(i);
24
25     cout << "the value of i is " << i << endl;   // output is 3
26     return 0;
27   }
28
29   // pass by value, x will not be effected in main function
30   void one(int x) {
31     x = 1;
32   }
33
34   // pass by reference, x will be effected in main function
35   void two(int *x) {
36     *x = 2;
37   }
38
39   // pass by reference, x will be effected in main function
40   void three(int &x) {
41     x = 3;
42   }

The output from above is:

the value of i is 0
the value of i is 0
the value of i is 2
the value of i is 3


source code here.