From: Eric Bambach <eric@cisu.net>
To: Suciu Flavius <suciuflavius@tiscali.de>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: different access to structs
Date: Mon, 4 Oct 2004 10:00:50 -0500 [thread overview]
Message-ID: <200410041000.50040.eric@cisu.net> (raw)
In-Reply-To: <cjr9n9$3g1$1@sea.gmane.org>
On Monday 04 October 2004 02:57 pm, you wrote:
> p.boehm@d-trust.net wrote:
> > Hi,
> > in last time I played a little bit with structures. There are two types
> > of access to it and I want to know where are the differences.
> > Or better: when it makes sense to use type one and when type two ?
> >
> > struct xy {
> > int id;
> > char *name;
> > };
> >
> > struct abc {
> > int id;
> > char *name;
> > struct xy st_name; /* <-- type 1 ( ...via name(=address)
> > ) */ struct xy *st_ptr; /* <-- type 2 ( ...via
> > pointer(=address) ) */ };
The memory requirements increase for type 1 as you are declaring a structure
inside a structure, whereas type 2 it is just a pointer. You can declare 1000
abc strcuts and have st_ptr point to one structure(or many if you wish) while
with 1000 abc structs, each will have its own private copy of st_name.
Furthermore, as I hinted at you can change st_ptr at any time you wish to any
xy structure while st_name will always point to one xy.id and one xy.name.
HTH.
In fact, can someone now explain to me why his program didn't crash from an
uninitialized pointer (struct xy *) st_ptr in abc? Is this a special case of
scructs, because that pointer looks pretty dangling to me....
> > void init_abc(struct abc *pt) { /* pt points to ptr ... */
> > pt->id=0;
> > pt->name="test";
> >
> > pt->st_name.id=0; /* <-- type 1 (access via name of
> > variable) */ pt->st_name.name="test";
> >
> > pt->st_ptr->id=0; /* <-- type 2 (access via pointer) */
> > pt->st_ptr->name="test";
> > }
> >
> > void main(void) {
> > struct abc ptr; /* name of struct = ptr */
> >
> > init_abc(&ptr); /* call init_abc(<address of ptr>) */
> > }
> >
> > thanks in advance!
> > pb
> >
> > -
> > To unsubscribe from this list: send the line "unsubscribe
> > linux-c-programming" in the body of a message to
> > majordomo@vger.kernel.org
> > More majordomo info at http://vger.kernel.org/majordomo-info.html
>
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
--
-EB
prev parent reply other threads:[~2004-10-04 15:00 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-10-04 10:15 different access to structs p.boehm
2004-10-04 19:57 ` Suciu Flavius
2004-10-04 15:00 ` Eric Bambach [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200410041000.50040.eric@cisu.net \
--to=eric@cisu.net \
--cc=linux-c-programming@vger.kernel.org \
--cc=suciuflavius@tiscali.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).