Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Help on this typedef Date: Thu, 18 May 2017 09:35:26 -0700 Organization: None to speak of Lines: 43 Message-ID: References: <8ad1e08b-bdf9-4baa-a8b4-63bf48fb3e3d@googlegroups.com> <7828b0dd-de46-40a2-9636-81117d1f17f6@googlegroups.com> <799899ab-ccf3-49a4-a928-d405dc142fff@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="3d325f4eef7918eb48bf3c8d42ea00ac"; logging-data="3238"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/zd7LpUYYrKltvRVv7zU36" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:NDs/gPgFuj0U8gbM1d58SUY193k= sha1:geFy5Oo2sZTxtipZ+nEvikvna8I= Xref: csiph.com comp.lang.c:110312 joel.rees@gmail.com writes: > On Monday, May 15, 2017 at 11:23:23 PM UTC+9, Tim Rentsch wrote: [...] >> Ah. In that case I must demur regarding part of your later >> statement. An access through an array type of extent 1 is >> not definedly allowed to access elements after the element >> at index 0. > > typedef string_header_s struct > { short length; /* limited length */ > char text[ 1 ]; > } string_header_t; That's called the "struct hack", and it was always of questionable validity. You can likely get away with allocating extra memory and referring to, for example, obj.text[10], but the behavior is undefined. C99 introduced flexible array members, described in N1570 6.7.2.1p18, as a well-defined replacement for the struct hack. See also question 2.6 of the comp.lang.c FAQ, . > And before you C++ types came around, we just left the array size > out: char text[]. But that was declared in all cases to be equivalent > to char *, so it means something else, now. And if that's what you > mean in such a header, you say char *, instead. Prior to C99's instroduction of flexible array members, defining a struct member as `char text[]` was invalid. And no, `char text[]` is in no way equivalent to `char *text`. See section 6 of the comp.lang.c FAQ. You can define a pointer member, but then you have to allocate memory for it to point to. With an array member, the memory is allocated within the struct object itself. [...] -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"