Pointers and Arrays

Pointers

Every object created in a C++ program, from a simple int to an instance of the most complex class, must be stored somewhere in the computers memory. A special data type, known as a pointer is used to hold the address of an object. Pointers are declared using the following notation:

int *pointerToint;

In this context the * character means pointer, so pointerToint is not an int itself, but rather a means of getting at an int stored somewhere in memory. In order to point a pointer at a particular object, the location of the object in memory can be obtained using the address-of operator, &

Int myint, *myintPtr; myintPtr= &myint;

Given a pointer to an object, the original object can be obtained using the * operator as follows:

int myint,

*myintPtr; myint = 4;

&myintptr = &myint

Note that in the above example, myint has been changed to 3, because it was the object to which myintPtr referred. There is no generic pointer data type in C++, although it is possible to get close to such an idea using a void pointer: *pointerToNothinginParticular; Although there are some situations where such pointers are required, in the vast majority of cases, the use of void pointers is strongly discouraged, as they can be used to defeat the C++ built in data type checking. Because void is not a proper data type, a void pointer must be cast to a typed pointer before it can be used.

Int i = (int*)pointerToNothingInParticular;

Arrays

One common use of pointers is to manipulate arrays.An array is a sequence of identical objects packed together in memory. Text strings are an example of an array. Arrays must be declared with a fixed size, as shown below: 

Int myintegers[14]; //an array of 14 integers

int myinteghers[10][5]// 10 arrays, each with 5 element

To access an object stored in an array, the subscript operator, [ ], is used, as follows:

myintegers[0]=10

myintegers[11]=1;

grid[7][3]='a';

Array indices (the numbers in the square brackets) are zero-based, so the elements of myintegers are numbered from 0 to 13, not from 1 to 14. simple arrays provide no bounds-checking: it is possible to attempt something likex=myintegers[201];. Also once defined at a particular size, it is difficult and time-consuming to enlarge an array. These limitations are overcome by the C++ standard library std:: vector class.

The name of the array (myintegers in the example above)is actually a pointer which points to (i.e., holds the address of) the first element of the array. Given a pointer to the first item in an array, it is possible to manipulate the entire array as shown below:

         // get total of myintegers array, using array subscripting

for (int counter=0; counter<14; counter++) total+= myintegers[counter];

//or using pointers

int total=0;

int* current=myintegers;

for(int counter=0;

{

counter<14; counter++)

total += *current;

current++;

}

When used on a pointer, arithmetic operators like the increment operator (++) will take into account the size of the data being pointed to. In this way current++; causes the pointer current to point to the next int in memory, and not the next memory location (an int may occupy two or more memory locations). In the example above, the use of a pointer is hardly necessary: the size of the array is known. Often however, this is not the case. C arrays do not hold any information regarding the size of the array, which can lead to problems when pointers to arrays are passed to functions. To get around this, array sentinels are often used. A sentinel is normally an invalid array element whose sole purpose is to mark the end of the array. For a C string,this sentinel value is the NUL character (\0). Knowing this, the following function allows the length of a string to be calculated:

int strlen(const char* string)

{

int returnSize=1;

while ( *(string++));

returnedSize++;

return returnedSize;

}

Note the use of the implicit comparison with zero in the while statement.