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


Groups > comp.lang.c > #164692

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

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: on looking up a word in a table, pointer question
Date 2022-01-28 22:23 +0000
Organization A noiseless patient Spider
Message-ID <87h79nzehy.fsf@bsb.me.uk> (permalink)
References <86k0ejbly8.fsf@levado.to>

Show all headers | View raw


Meredith Montgomery <mmontgomery@levado.to> writes:

> 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:
>
> 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}
> };
>
>
> Here's a procedure to lookup a word in this table:
>
> 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 */
> }
>
> Notice how I'm stopping when p->word is not NULL.  This happens when p
> gets to the last element of that array, which is terminated with a zero
> automatically by the compiler.  (Just like a c-string.)  (I suppose.)

Eek!  No, that does not happen.  You will be tricked by the fact that
your code will often work because zeros are so common, but you need to
add a final

  { 0, 0 }

entry to the table.

> 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.  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 *.  
>
> %make keywords2
> cc -x c -g -std=c99 -pedantic-errors   -c -o keywords2.o keywords2.c
> keywords2.c: In function ‘lookup’:
> keywords2.c:48:23: error: invalid operands to binary != (have ‘struct kw’ and ‘void *’)
>    48 |   for ( p = table; *p != NULL; p++) {
>       |                    ~~ ^~
>       |                    |
>       |                    struct kw
> make: *** [<builtin>: keywords2.o] Error 1
> %
>
> 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.

There is nothing wrong with using p->word.  You could add a line

  { "", -1 }

and test for p->count >= 0 because any impossible value like a negative
count can be used as a sentinel (as these things are known), but using a
null pointer where a string is expected will be recognised for what it
is by everyone reading your code.

You can, just about, test for *p being "all zeros" but it's not worth
trying.  For one thing structs can have padding that might be non-zero!

-- 
Ben.

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