Arrays of Pointers


1.Arrays of Pointers

Pointers can be arrayed like any other data type. For example,
int *ipa[10];
declares ipa as an array of 10 integer pointers. Thus, each element in ipa holds a pointer to an int value. To assign the address of an int variable called var to the third element of ipa, you would write
ipa[2] = &var;
Remember, ipa is an array of integer pointers. The only values that its array elements can hold are the addresses of integer variables. This is why var is preceded by the & operator. Using the ipa array to assign the value of var to an int variable called x, you would write:
x = *ipa[2];
Because the address of var is stored at ipa[2], applying the * operator to this index causes the value of var to be obtained. Like other arrays, arrays of pointers can be initialized. A common use for initialized pointer arrays is to hold pointers to strings. For example, to create a function that will output a fortune, you can define a number of different messages in a pointer array, as shown here:
char *fortunes[] = {
"Soon, you will come into some money.\n",
"A new love will enter your life.\n",
"You will live long and prosper.\n",
"Now is a good time to invest for the future.\n",
"A close friend will ask for a favor.\n"
};
Remember, C++ stores all string literals in the string table associated with your program, so the array need only store pointers to the strings. Thus, to print the second message, use a statement like this:
cout << fortunes[1];
An entire "fortune cookie" program is shown here. It uses rand( ) to generate a random number. It then uses the modulus operator to obtain a number between 0 and 4, which it uses to index the array.
#include <iostream>
#include <cstdlib>
#include <conio.h>
using namespace std;
char *fortunes[] = {
"Soon, you will come into some money.\n",
"A new love will enter your life.\n",
"You will live long and prosper.\n",
"Now is a good time to invest for the future.\n",
"A close friend will ask for a favor.\n"
};
int main()
{
int chance;
cout << "To see your fortune, press a key: ";
// randomize the random number generator
while(!kbhit()) rand();
cout << '\n';
chance = rand();
chance = chance % 5;
cout << fortunes[chance];
return 0;
}
Notice the while loop in the program, which calls rand( ) repeatedly until a key is pressed. Because the rand( ) function always generates the same sequence of random numbers, it is important to have some way for the program to start using this sequence at a random point. (Otherwise, the same fortune will be given each time the program is run.) This is achieved by repeated calls to rand( ). When the user presses a key, the loop stops at a random point in the sequence, and the fortune is displayed on the screen. Remember, kbhit( ) is a common extension provided by many compilers, but it is not defined by C++. The next example uses a two-dimensional array of pointers to create the skeleton of a program that displays a syntax reminder for the C++ keywords. This program initializes a list of string pointers. The first dimension points to a C++ keyword, and the second dimension points to a short description of the keyword. The list is terminated by two null strings. These nulls are used to mark the end of the list. The user enters a keyword, and the program displays the description. As you can see, only a few keywords have been listed. The expansion of the list is left to you, as an exercise.
// A simple C++ keyword synopsis program.
#include <iostream>
#include <cstring>
using namespace std;
char *keyword[][2] = {
"for", "for(initialization; condition; increment)",
"if", "if(condition) ... else ...",
"switch", "switch(value) { case-list }",
"while", "while(condition) ...",
// add the rest of the C++ keywords here
"", "" // terminate the list with nulls
};
int main()
{
char str[80];
int i;
cout << "Enter keyword: ";
cin >> str;
// display syntax
for(i=0; *keyword[i][0]; i++)
if(!strcmp(keyword[i][0], str))
cout << keyword[i][1];
return 0;
}

Here is a sample run.
Enter keyword: for
for(initialization; condition; increment)
In the program, notice the expression controlling the for loop. It causes the loop to terminate when keyword[i][0] contains a pointer that points to a null, which is a false value. Thus, when the loop encounters the null strings at the end of the pointer array, the loop stops.
Share on Google Plus