Pointers as Operands, Pointers to Arrays and Arrays of Pointers

Pointers as Operands

While pointers are integers, they are special integers and not all integral operators can be used on pointers. For example the operators: *, / and % do not work on pointers while ++(), --(), + and - do but they only work in limited cases.

The question should arise: WHAT DOES: ptr + 1 MEAN? The answer depends on the data type to which ptr is a pointer. If ptr is a pointer to a char and it points to the address 1000, then ptr + 1 is 1001. However, if ptr is a pointer to a float and it points to the address 1000 then ptr + 1 is 1004 while ptr - 1 is 996. See pointerAddition.cpp

An important question is then, after the addition or the subtraction: ARE THESE THEN ADDRESSES OF THE SAME TYPE? They may not be and therefore these values may not be assigned to a pointer of the same type! SO WHAT GOOD ARE THESE VALUES? When pointers and arrays are connected, the values ptr + 1 or ptr - 1 may represent an element in an array. That is, if ptr points to an element of an array, then ptr + 1 may be the address of the next element in the array. In a similar case, ptr - 1 may be the address of the previous element of the array to which ptr points.

The same concern should be held with respect to ++(), ()++, --() and ()-- as with: ptr + 1 and ptr - 1 except that the values in ptr is changed in the former. However, with the increment or the decrement operators, the result may change the value in the pointer to an address that is not the address of data of the correct type. Therefore these should not be allowable operations unless the results are also a pointer of the same type.

Further ptr1 + ptr2 is not meaningful and ptr1 - ptr2 is only meaningful when they are pointers to different elements within the same array. Using the value ptr1 - ptr2, the number of array elements between the elements to which these pointers point may be determined. See TESTPTR.CPP

Pointers to Arrays

It is possible to connect pointers to arrays in the following manner:

image from book

 float Gl[100]; float *ptrGl; ptrGl = Gl; 

image from book

Notice that the statement above assigns the value of Gl to ptrGl. The question may arise: what is this statement doing? What happens is that Gl is a pointer that contains the address of the first element of the array. Actually this last statement is the same as:

image from book

 ptrGl = &Gl[0]; 

image from book

The major differences between the pointer to the array and the array name are:

  1. An array name always refers to the same place in memory (i.e. the array name is a constant pointer) while the pointer may refer to several different places in memory.

  2. It is possible to perform increment and decrement operations on pointers but not on array names.

However, it is possible to use either to denote an element. For example:

image from book


image from book

is the same as

image from book

 *(ptrGl + 10) 

image from book

Note the need for the parenthesis and the use of the + operator. This is required because of the order of precedence of these two operators. See ARRAYPTR.CPP. The use of pointers to arrays should produce tighter and faster code than using array names.

Pointers and the Relational Operator

Not only can the arithmetic operators be used with pointers but the relational operators may be used with pointers of the same base type as operands. While this is true, the use of relational operators like the arithmetic operators is limited to those pointers that point to the same array. For example expressions like the following:

image from book

 ptr1 < ptr2 ptr1 >= ptr2 

image from book

will depend on the fact that these pointers would each point to elements of the same array. If they do not, then no prediction may be made as to how these expressions may be evaluated. See relationalptr.cpp

Arrays of Pointers

When a string literal like: "Today" is used in a program, it is stored in the string table. As such then, a pointer may be used to point to this string. While such pointers may be used, they should not be used to modify the contents of such an array as pointers to arrays in general may.

Not only is there a connection between arrays and pointers, it is possible to have an array of pointers. For example:

image from book

 char *dayName[ ] = {"Sunday", "Monday", "Tuesday",                       "Wednesday", "Thursday", "Friday", "Saturday"}; 

image from book

defines dayName to be the name of an array of pointer to the char data type. See DATE.CPP

Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net