Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #164690

Re: on looking up a word in a table, pointer question

From Kaz Kylheku <480-992-1380@kylheku.com>
Newsgroups comp.lang.c
Subject Re: on looking up a word in a table, pointer question
Date 2022-01-28 22:07 +0000
Organization A noiseless patient Spider
Message-ID <20220128135506.266@kylheku.com> (permalink)
References <86k0ejbly8.fsf@levado.to>

Show all headers | View raw


On 2022-01-28, Meredith Montgomery <mmontgomery@levado.to> wrote:
> I show the full program at the end of this message, but I bet you don't
> need the full program.  
>
> Here's a table of C keywords:
>
> --8<---------------cut here---------------start------------->8---
> struct kw {
>   char *word; 
>   int count;
> };
>
> struct kw table[] = {
>   {"break", 0},
>   {"case", 0},
>   {"char", 0},
>   {"const", 0},
>   {"int", 0},
>   {"continue", 0},
>   {"default", 0},
>   {"unsigned", 0},
>   {"void", 0},
>   {"volatile", 0},
>   {"while", 0}
> };
> --8<---------------cut here---------------end--------------->8---
>
> Here's a procedure to lookup a word in this table:
>
> --8<---------------cut here---------------start------------->8---
> struct kw * lookup(char *word, struct kw * table, int n) {
>   struct kw *p;
>
>   for ( p = table; p->word != NULL; p++) {
>     if (strcmp(word, p->word) == 0)
>       return p;
>   }
>
>   return NULL; /* not found */
> }
> --8<---------------cut here---------------end--------------->8---
>
> Notice how I'm stopping when p->word is not NULL.

So you need an entry for that in the array

    {"while", 0},
    {NULL, 0}
  };

> };
> This happens when p gets to the last element.

"gets" is deprecated in C, please say "moves" or "mosies on over to" or
some other more precise word. :)

> to the last element of that array, which is terminated with a zero
> automatically by the compiler.  (Just like a c-string.)  (I suppose.)
>
> So far so good. 
>
> But this is not what I wrote first.  I'm stepping p through each element
> in the table, so I know that eventually p points to the last element of
> the array which is a certain zero-thing.

The last element of the array is the {"while", 0} entry.

So the more natural comparison
> to me would be something like 
>
>   *p != NULL
>
> but the compiler won't let me because it says that *p has type 
>
>   struct kw, 
>
> which cannot be compared with 
>
>   void *.  

That could be arrangd, but provides no benefit:

  int null_kw(struct kw kw)
  {
     return kw.word == NULL && kw.count == 0;
  }

So then, provided you have the {NULL, 0} entry in the table. the loop
could terminate like this:

  for (p = table; !null_kw(*p); p++)

That's a lot of ceremony for something simple. Or even

  struct kw NULL_kw = { NULL, 0 };

  for (p = table; !kw_compare(*p, NULL_kw); p++)

where we have this:

  int kw_compare(struct kw left, struct kw right)
  {
     return ((left.word == NULL && right.word == NULL) ||
             (left.word != NULL && right.word != NULL &&
              strcmp(left.word, right.word) == 0)) &&
            left.count == right.count;
  }

All these kinds of idioms have their right time and place; probably
not here though in the simple loop through a table of keywords.

> This makes perfect sense.  But how could I detect that p has reached the
> end of the table in a more natural way than p->word?  What is the
> standard way to write this?  Thank you.

Comparing p->word to NULL and ensuring there such an entry at the end
of the array is natural.

Another way is this:

  /* caution: this macro invites an array_size(pointer) bug */

  #define num_elements(array) (sizeof array / sizeof array[0])

  /* ... */

  for (i = 0; i < num_elements(table); i++) {
     if (strcmp(table[i], word) == 0)
        return &table[i];
  }

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

on looking up a word in a table, pointer question Meredith Montgomery <mmontgomery@levado.to> - 2022-01-28 18:16 -0300
  Re: on looking up a word in a table, pointer question scott@slp53.sl.home (Scott Lurndal) - 2022-01-28 21:29 +0000
  Re: on looking up a word in a table, pointer question Meredith Montgomery <mmontgomery@levado.to> - 2022-01-28 18:49 -0300
  Re: on looking up a word in a table, pointer question Kaz Kylheku <480-992-1380@kylheku.com> - 2022-01-28 22:07 +0000
    Re: on looking up a word in a table, pointer question Meredith Montgomery <mmontgomery@levado.to> - 2022-01-28 22:03 -0300
  Re: on looking up a word in a table, pointer question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-01-28 22:23 +0000
    Re: on looking up a word in a table, pointer question Meredith Montgomery <mmontgomery@levado.to> - 2022-01-28 22:05 -0300
    Re: on looking up a word in a table, pointer question James Kuyper <jameskuyper@alumni.caltech.edu> - 2022-01-28 21:10 -0500
  Re: on looking up a word in a table, pointer question Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-01-28 22:27 +0000
    Re: on looking up a word in a table, pointer question Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2022-01-28 23:18 +0000
      Re: on looking up a word in a table, pointer question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-01-29 02:32 +0000
        Re: on looking up a word in a table, pointer question Meredith Montgomery <mmontgomery@levado.to> - 2022-01-30 10:23 -0300
          Re: on looking up a word in a table, pointer question Bart <bc@freeuk.com> - 2022-01-30 13:44 +0000
    Re: on looking up a word in a table, pointer question Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2022-01-28 23:23 +0000

csiph-web