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


Groups > comp.lang.c > #164699

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 22:03 -0300
Organization Aioe.org NNTP Server
Message-ID <86ilu39wul.fsf@levado.to> (permalink)
References <86k0ejbly8.fsf@levado.to> <20220128135506.266@kylheku.com>

Show all headers | View raw


Kaz Kylheku <480-992-1380@kylheku.com> writes:

> 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}
>   };

Aha!  That's what I had not realized.

>> };
>> 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. :)

Lol.  Will do.  :-) 

>> 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.

Yes, I was bitten there.  Thanks for pointing it out.

> 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++)

Lol.  I've done this once.  Lol.  It's really such a bad solution.  Let
me see if I can look up when I did this.  Here.

typedef struct Pair {
  uint64_t p;
  uint64_t q;
} Pair;

#define IS_NOT_NIL_PAIR(P) (P.p != 0 && P.q != 0)

int main() {
  Pair N;
  for (N = composites(); IS_NOT_NIL_PAIR(N); N = composites()) {
    printf("Pair (%ld, %ld) produces N = %ld\n", N.p, N.q, N.p * N.q);
  }
  printf("Done\n");
  return 0;
}

Pair composites(void) {
  static unsigned int i = 0;
  static unsigned int j = 1;
  
  Pair N;
  N.p = primes(i);
  N.q = primes(j);

  if (i == (primes_size() - 1) && j == primes_size()) {
    return (Pair) {0, 0};
  }

  j = j + 1;

  if (j == (primes_size())) { 
    i = i + 1; 
    j = i + 1; 
  }
  
  return N;
}

Oh, boy.  I really didn't know any better at that time.  

My case up there wasn't quite a table, but I wanted to somehow specify
that at some point I wasn't going to generate more composites and,
because I didn't want to work with pointers, I had to return a struct.
I guess I still don't know how to do any better here.  (It's not a
table.)

I think I should improve this a by creating a nullPair --- like Kaz
Kylheku was doing.  So in case of termination or error, I just return
this nullPair.

[...]

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