Navigation(slides)

6.4 Pointers to Structures 指向结构体的指针

To illustrate some of the considerations involved with pointers to and arrays of structures, let us write the keyword-counting program again, this time using pointers instead of array indices.

为了进一步说明指向结构体的指针和结构体数组,让我们重新编写关键字统计程序。这次使用指针,而不是用数组下标。

The external declaration of keytab need not change, but main and binsearch do need modification.

keytab的外部声明不需要修改,但是main函数和binsearch函数需要修改。

   1    #include <stdio.h>
   2    #include <ctype.h>
   3    #include <string.h>
   4    #define MAXWORD 100
   5 
   6    int getword(char *, int);
   7    struct key *binsearch(char *, struct key *, int);
   8 
   9    /* count C keywords; pointer version */
  10    main()
  11    {
  12        char word[MAXWORD];
  13        struct key *p;
  14 
  15        while (getword(word, MAXWORD) != EOF)
  16            if (isalpha(word[0]))
  17                if ((p=binsearch(word, keytab, NKEYS)) != NULL)
  18                    p->count++;
  19        for (p = keytab; p < keytab + NKEYS; p++)
  20            if (p->count > 0)
  21                printf("%4d %s\n", p->count, p->word);
  22        return 0;
  23    }
  24 
  25    /* binsearch: find word in tab[0]...tab[n-1] */
  26    struct key *binsearch(char *word, struck key *tab, int n)
  27    {
  28        int cond;
  29        struct key *low = &tab[0];
  30        struct key *high = &tab[n];
  31        struct key *mid;
  32 
  33        while (low < high) {
  34            mid = low + (high-low) / 2;
  35            if ((cond = strcmp(word, mid->word)) < 0)
  36                high = mid;
  37            else if (cond > 0)
  38                low = mid + 1;
  39            else
  40                return mid;
  41        }
  42        return NULL;
  43    }

There are several things worthy of note here. First, the declaration of binsearch must indicate that it returns a pointer to struct key instead of an integer; this is declared both in the function prototype and in binsearch. If binsearch finds the word, it returns a pointer to it; if it fails, it returns NULL.

这里有几点值得注意。首先,binsearch函数在声明:它返回的值的类型是一个指向struct key类型的指针,而非整型;这个在函数原型和binsearch函数中都要声明。如果binsearch找到与输入单词匹配的数组元素,它将返回一个指向该元素的指针,否则返回NULL。

Second, the elements of keytab are now accessed by pointers. This requires significant changes in binsearch.

The initializers for low and high are now pointers to the beginning and just past the end of the table.

The computation of the middle element can no longer be simply

   mid = (low+high) / 2    /* WRONG */

because the addition of pointers is illegal. Subtraction is legal, however, so high-low is the number of elements, and thus

   mid = low + (high-low) / 2

sets mid to the element halfway between low and high.

The most important change is to adjust the algorithm to make sure that it does not generate an illegal pointer or attempt to access an element outside the array. The problem is that &tab[-1] and &tab[n] are both outside the limits of the array tab. The former is strictly illegal, and it is illegal to dereference the latter. The language definition does guarantee, however, that pointer arithmetic that involves the first element beyond the end of an array (that is, &tab[n]) will work correctly.

In main we wrote

   for (p = keytab; p < keytab + NKEYS; p++)

If p is a pointer to a structure, arithmetic on p takes into account the size of the structure, so p++ increments p by the correct amount to get the next element of the array of structures, and the test stops the loop at the right time.

Don't assume, however, that the size of a structure is the sum of the sizes of its members. Because of alignment requirements for different objects, there may be unnamed "holes" in a structure. Thus, for instance, if a char is one byte and an int four bytes, the structure

   struct {
       char c;
       int i;
   };

might well require eight bytes, not five. The sizeof operator returns the proper value.

Finally, an aside on program format: when a function returns a complicated type like a structure pointer, as in

   struct key *binsearch(char *word, struct key *tab, int n)

the function name can be hard to see, and to find with a text editor. Accordingly an alternate style is sometimes used:

   struct key *
   binsearch(char *word, struct key *tab, int n)

This is a matter of personal taste; pick the form you like and hold to it.

ch3n2k.com | Copyright (c) 2004-2020 czk.