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


Groups > comp.lang.c > #55186

Re: Pointer to structure

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Pointer to structure
Date Sun, 21 Dec 2014 13:16:52 +0000
Organization A noiseless patient Spider
Lines 108
Message-ID <87ppbdp2gr.fsf@bsb.me.uk> (permalink)
References <6e358290-996c-44f8-9026-4b9cf6ca9798@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain
Injection-Info mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="15911"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18vF8g3CEB9Fi0NSI5CzFTnA8KVrul1j0A="
Cancel-Lock sha1:NDSUjN9Xr0UvrK/aaNZO9Ggyayw= sha1:K3zzh3vk0mBx31uEfHlp6Y+4c7Q=
X-BSB-Auth 1.99509fec673908b5ec56.20141221131652GMT.87ppbdp2gr.fsf@bsb.me.uk
Xref csiph.com comp.lang.c:55186

Show key headers only | View raw


Dexterr <dejangru@gmail.com> writes:

> typedef struct _PascalString_

That's a reserved identifier (basically the implementation may decide
what it means in a way that messes with your meaning).  It's best to
avoid all names that start with an _ (though not all such names are
reserved).

> {
>   int size;
>   char* string;
> }PString;

There's a big unstated problem here.  'Pascal string' is not a
well-defined term.  String variables did not really exist in the
original Pascal where all you had were packed arrays of char, and this
definition persisted into ISO standard Pascal.  Then there are a large
number of Pascal-like languages, almost all of which introduced some
more useful form of string type.

UCSD Pascal was one of the first and may well have been the model for
all the others.  The system included a well-defined virtual machine
(p-code) that does specify the representation of a string.  It's
possible that with the exception of standard Pascal, all variations that
include a string type use the same representation but, if not, there may
be other good meanings for the term.

Anyway, a UCSD Pascal string looks like this is C:

  typedef struct {
      unsigned char length;
      char string_data[];
  } UCSD_P_String;

and a function to allocate one from a C string would look like this:

  UCSD_P_String *make_p_string(const char *str)
  {
       size_t len = strlen(str);
       UCSD_P_String *pstr = 0;
       if (len < 256 && (pstr = malloc(sizeof *pstr + len)) != NULL) {
            pstr->length = len;
            memcpy(pstr->string_data, str, len);
       }
       return pstr;
  }

though I would be tempted to allocate one more byte and copy the null
byte across.  Otherwise, you must remember to use the length properly
all the time.  For example, to print one, you need to say

  printf("%.*s", (int)pstr->length, pstr->string_data);

> int main()
> {
>   PString* str = malloc(sizeof(struct _PascalString_));
>     
>   str = malloc(sizeof(struct _PascalString_));

A neater (and less error-prone) syntax is:

   str = malloc(sizeof *str);
  
>   str->string = malloc(sizeof(char) * str->size);
>   
>   
>   str->size = 9;
>   str->string = malloc(sizeof(char) * str->size);
>   str->string = "Tennessee";

You allocate things multiple times, and then you overwrite the pointer
to the space you've allocated with a pointer to a statically allocated
string.

>   printf("%s\n", str->string);
>     
>   return 0;
> }
>
> I have a problem with understanding a structures. For instance, this
> code works, but usually we sholud write something like this:
>
>  PString some_str; 
>
>  PString *str;
>
>  str = &some_str;
>
>  str->string = "Tennessee";
>
> Why is that ?

No, that's not usual, it's just a rather odd alternative.  If you can
(for reasons that relate to the overall design of the program) declare,
rather than allocate, your string objects, then there is no need to use
a pointer to them:

  PString some_str; 
  some_str.length = sizeof "Tennessee" - 1;
  some_str.string = "Tennessee";

or even

  PString some_str = { sizeof "Tennessee" - 1, "Tennessee" };

-- 
Ben.

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