Re: Structures of indefinite size

Discussion in 'C Programming' started by Dave Thompson, Jun 26, 2003.

  1. On Fri, 20 Jun 2003 10:22:05 -0400, Eric Sosman <>
    wrote:

    > Ben Pfaff wrote:
    > >
    > > That said, the idea of including a variable-length array in a C
    > > structure has a long and spotted history. The typical way to do
    > > it in C90 is [struct hack ...] In C99, [FAM...]


    > A third method that works with both C90 and C99 is
    > to omit the `text' part of the struct altogether and just
    > rely on the knowledge that the characters follow the
    > struct. A macro can hide the details:
    >
    > struct string {
    > unsigned short capacity;
    > unsigned short length;
    > };
    > #define TEXT(stringptr) (char*)((stringptr) + 1)
    >

    Just to note: this method is only guaranteed to work for (the three
    flavors of) char, because they cannot have nontrivial alignment; it
    can be adjusted for other types, but not as easily as the struct-hack
    or FAM methods. The OP did ask for strings (which must be char, or
    maybe wchar_t) although the subject line is phrased more generally.

    - David.Thompson1 at worldnet.att.net
    Dave Thompson, Jun 26, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?T2xlZw==?=

    Session timeout indefinite

    =?Utf-8?B?T2xlZw==?=, Dec 9, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    741
    Patrice
    Dec 9, 2005
  2. Jenny
    Replies:
    5
    Views:
    809
    Nigel Wade
    Sep 16, 2004
  3. Alfonso Morra
    Replies:
    11
    Views:
    701
    Emmanuel Delahaye
    Sep 24, 2005
  4. Replies:
    3
    Views:
    269
    Jakob Bieling
    Mar 27, 2006
  5. Replies:
    6
    Views:
    320
    Diego Martins
    Mar 30, 2006
Loading...

Share This Page