Ethical Hacking Programming, Blogging, Hosting, All Computer Software, PC Software Download, JAVA in hindi, HTML, PHP, C, C++, Free Learning, Software's Download, Technical Videos, Technical Tricks and Tips, How Make Money

Arrays of Pointers in C programming Class 30

8.4 Arrays of Pointers


Since pointers are themselves variables, they can be stored in arrays just as other variables can. For example, an array of N pointers to ints has the following syntax.

int *parray[N];

Each pointer in an array of pointers behaves as any ordinary pointer would. They might point to an object, to NULL, to an illegal memory location, or to an array.

double val = 9.7;

double array[] = { 3.2, 4.3, 5.4 };

double *pa[] = { &val, array+1, NULL };

In the above example, element pa[i] is a pointer to a double, and *pa[i] is the double variable that it points to. The dereferenced *pa[0] is equal to 9.7, and *pa[1] is 4.3, but pa[2] is equal to NULL and may not be dereferenced.
If an element in an array of pointers also points to an array, the elements of the pointed-to array may be accessed in a variety of di erent ways. Consider the following example.

int a1[] = { 1, 2, 3, 4 };
int a2[] = { 5, 6, 7 };
int *pa[] = { a1, a2 }; /* pa stores pointers to beginning of each array. */
int **pp = pa; /* Pointer-to-a-pointer holds address of beginning of pa. */
int *p = pa[1]; /* Pointer to the second array in pa. */
int val;
val = pa[1][1]; /* equivalent operations: val = 6 */
val = pp[1][1];

val = *(pa[1] + 1);
val = *(pp[1] + 1);
val = *(*(pp+1) + 1));
val = p[1];

Notice that in an expression pa and pp are equivalent, but the di erence is that pa is an array name and pp is a pointer. That is, pp is a variable and pa is not.
Arrays of pointers are useful for grouping related pointers together. Typically these are one of three types: pointers to large objects (such as structs), pointers to arrays, or pointers to functions.

For the first two categories,3 most interesting applications of pointer arrays involve the use of dynamic memory, which is discussed in Chapter 9. However, a simple example of an array of pointers to arrays is the following program which obtains a number from the user and prints the name of the corresponding month.


1 #include <stdio.h>

2

3   int main(void)

4   {

5 char *months[ ] = { "Illegal", "January", "February", "March",
6 "April", "May", "June", "July", "August", "September",

7 "October", "November", "December" };
8 int i, j;

9

10 printf("Input an integer between 1 and 12: ");

11 scanf("%d", &i);

12 if (i<1 | | i>12) i=0;

13

14 printf("Month number %d is %s.\n", i, months[i]); /* print string */

15
16 printf("The letters of the month are: ");

17 for (j = 0; months[i][j] != ’\0’; ++j) /* access elements using [ ][ ] */

18 printf("%c ", months[i][j]);

19 }


A common application for an array of function pointers is the construction of a “dispatch” table, which is used to invoke a particular function depending on the value of an array index. Each function in the array must have the same interface. The following example shows the syntax for defining an array of function pointers.

int (*pf[10])(char *);

This statement defines a variable pf, which is an array of pointers to functions where each function fits the interface specification: takes a char * argument and returns an int.

The examples given below show the use of an array of function pointers to perform simple arithmetic functions. Each function has two double logic signatures and gives one double.


1 #include <stdio.h>

2   #include <assert.h>

3

4 double add(double a, double b) { return a + b; } 5 double sub(double a, double b) { return a − b; }
6   double mult(double a, double b) { return a * b; }

7   double div(double a, double b) { assert(b != 0.0); return a / b; }

8

9   int main(void)

10 {

11 int i;

12 double val1, val2;

13 double (*pf[ ])(double,double) = { add, sub, mult, div };

14
15 printf("Enter two floating-point values, and an integer between 0 and 3: ");

16 scanf("%lf%lf%d", &val1, &val2, &i);

17 if (i<0 | | i>3) i = 0;

18

19 printf("Performing operation %d on %3.2f and %3.2f equals %3.2f\n",

20 i, val1, val2, pf[i](val1, val2));

21 }


Note The first two categories, pointers to large objects and pointers to arrays, are actually of the same kind, where arrays are just one form of large object.

The declaration syntax for function pointers and arrays of function pointers rapidly becomes very complicated. A good mechanism for simplifying declarations is to break them up using typedef. The keyword typedef is used for creating new data-type names. For example, in the following declaration,

typedef char * String;

the name String becomes a synonym for char *, and can be used to define variables of this type.

String message = "This is a string.";

With regard to complicated function pointer declarations, di erent parts of the declaration can be given a name using typedef, so that the combined whole is more readable. For example, the declaration in the program above can be rewritten as

typedef double (*Arithmetic)(double,double);

Arithmetic pf[] = { add, sub, mult, div };

The name Arithmetic becomes a synonym for a function pointer of the specified type, and defining an array of such function pointers is simple. We discuss further uses of typedef in Sections 11.5 and 14.1.


Share:

No comments:

Post a Comment

Follow On YouTube