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


Groups > comp.lang.c > #164704

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

From James Kuyper <jameskuyper@alumni.caltech.edu>
Newsgroups comp.lang.c
Subject Re: on looking up a word in a table, pointer question
Date 2022-01-28 21:10 -0500
Organization A noiseless patient Spider
Message-ID <st27nh$gnb$1@dont-email.me> (permalink)
References <86k0ejbly8.fsf@levado.to> <87h79nzehy.fsf@bsb.me.uk>

Show all headers | View raw


On 1/28/22 17:23, Ben Bacarisse wrote:
> Meredith Montgomery <mmontgomery@levado.to> writes:
...
>> 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}
>> };
...
>> 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 }

Note, however, that something similar can happen. If he had specified
that table has 12 or more elements, "all subobjects that are not
initialized explicitly shall be initialized implicitly the same as
objects that have static storage duration." (6.7.9p19). For pointers,
that means a null pointer (6.7.9p10).

Of course, explicitly adding a {0,0} at the end is a better solution, as
it works regardless of the number of initializers.

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