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


Groups > comp.lang.c > #55248

Re: Pointer to structure

From glen herrmannsfeldt <gah@ugcs.caltech.edu>
Newsgroups comp.lang.c
Subject Re: Pointer to structure
Date 2014-12-22 00:50 +0000
Organization Aioe.org NNTP Server
Message-ID <m77prn$d50$1@speranza.aioe.org> (permalink)
References (1 earlier) <%Lylw.534246$CW3.84569@fx07.am4> <chine.bleu-B9C546.04393821122014@news.eternal-september.org> <Iuzlw.227520$p95.10761@fx38.am4> <chine.bleu-CBB10B.05052221122014@news.eternal-september.org> <lHzlw.145625$c73.56007@fx27.am4>

Show all headers | View raw


BartC <bc@freeuk.com> wrote:
> On 21/12/2014 13:05, Siri Crews wrote:

(snip, someone wrote)
>>> Odd then to use a whole 256 bytes for *every* string!

>> We didn't. The byte count could be up to 255 but the actual 
>> string size depending on need. 80 was a common size.
 
> In that case what *would* be an equivalent C data structure? Would it 
> need to be different for every possible string length?

There is a common C trick, likely UB in the standard, but usually
works, to declare an array, usually [1] at the end of the struct,
and then allocate it an apprropriate amount of memory.

struct ps {
   char length;
   char data[1];
   }

int n=80;
struct ps *x=malloc(sizeof(*x)-1+n);
 
> (The last time I used Pascal, we also used 80-character strings (but had 
> more than 64KB). I don't remember anything about a length field. Using 
> different sizes didn't work as Pascal's type system, although admirable 
> in some ways, would ensure that a 70-character string was incompatible 
> with an 80-character one!)

-- glen

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Pointer to structure Dexterr <dejangru@gmail.com> - 2014-12-21 03:16 -0800
  Re: Pointer to structure BartC <bc@freeuk.com> - 2014-12-21 12:09 +0000
    Re: Pointer to structure Siri Crews <chine.bleu@yahoo.com> - 2014-12-21 04:39 -0800
      Re: Pointer to structure BartC <bc@freeuk.com> - 2014-12-21 12:59 +0000
        Re: Pointer to structure Siri Crews <chine.bleu@yahoo.com> - 2014-12-21 05:05 -0800
          Re: Pointer to structure BartC <bc@freeuk.com> - 2014-12-21 13:12 +0000
            Re: Pointer to structure Siri Crews <chine.bleu@yahoo.com> - 2014-12-21 05:55 -0800
              Re: Pointer to structure BartC <bc@freeuk.com> - 2014-12-21 14:31 +0000
                Re: Pointer to structure Siri Crews <chine.bleu@yahoo.com> - 2014-12-21 06:47 -0800
                Re: Pointer to structure BartC <bc@freeuk.com> - 2014-12-21 20:10 +0000
                Re: Pointer to structure Geoff <geoff@invalid.invalid> - 2014-12-21 10:00 -0800
                Re: Pointer to structure Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-21 22:44 +0000
                Re: Pointer to structure raltbos@xs4all.nl (Richard Bos) - 2014-12-21 18:01 +0000
                Re: Pointer to structure BartC <bc@freeuk.com> - 2014-12-21 21:21 +0000
                Re: Pointer to structure raltbos@xs4all.nl (Richard Bos) - 2014-12-22 12:11 +0000
                Re: Pointer to structure Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-21 22:36 +0000
              Re: Pointer to structure Stephen Sprunk <stephen@sprunk.org> - 2014-12-27 17:56 -0600
                Re: Pointer to structure Siri Crews <chine.bleu@yahoo.com> - 2014-12-27 16:33 -0800
                Re: Pointer to structure Stephen Sprunk <stephen@sprunk.org> - 2015-01-15 12:29 -0600
            Re: Pointer to structure glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-12-22 00:50 +0000
              Re: Pointer to structure Richard Damon <Richard@Damon-Family.org> - 2014-12-21 20:19 -0500
                Re: Pointer to structure glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-12-22 06:23 +0000
                Re: Pointer to structure gazelle@shell.xmission.com (Kenny McCormack) - 2014-12-22 12:03 +0000
          Re: Pointer to structure gazelle@shell.xmission.com (Kenny McCormack) - 2014-12-21 13:20 +0000
            Re: Pointer to structure Siri Crews <chine.bleu@yahoo.com> - 2014-12-21 05:40 -0800
  Re: Pointer to structure Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-21 13:16 +0000
  Re: Pointer to structure Barry Schwarz <schwarzb@dqel.com> - 2014-12-21 10:02 -0800
  Re: Pointer to structure Keith Thompson <kst-u@mib.org> - 2014-12-21 12:25 -0800
    Re: Pointer to structure Dexterr <dejangru@gmail.com> - 2014-12-21 15:07 -0800

csiph-web