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


Groups > comp.lang.c > #164687

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

From Meredith Montgomery <mmontgomery@levado.to>
Newsgroups comp.lang.c
Subject Re: on looking up a word in a table, pointer question
Date 2022-01-28 18:49 -0300
Organization Aioe.org NNTP Server
Message-ID <865yq3bkf6.fsf@levado.to> (permalink)
References <86k0ejbly8.fsf@levado.to>

Show all headers | View raw


Sorry.  This post turned out to be a case of being tired.

> 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 */
> }

The reason the argument n is there is precisely to be the size of the
table so that we can write the for loop's test as 

  p < table + n

Here's a revise version of lookup():

--8<---------------cut here---------------start------------->8---
struct kw * lookup(char *word, struct kw * table, int n) {
  struct kw *p;
  for (p = table; p < table + n; p++) {
    if (strcmp(word, p->word) == 0)
      return p;
  }

  return NULL; /* not found */
}
--8<---------------cut here---------------end--------------->8---

This loop is pretty natural to me.  I was being bitten, too, by the fact
that in main() I had an array and in lookup() I had just a pointer to an
element of the table.  The NKEYS macro, therefore, doesn't work as I
expected inside lookup().  I was bitten there.

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