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


Groups > comp.lang.c > #164700

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:05 -0300
Organization Aioe.org NNTP Server
Message-ID <86a6ff9ws7.fsf@levado.to> (permalink)
References <86k0ejbly8.fsf@levado.to> <87h79nzehy.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

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

That's right.  Lesson learned!

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