From: Suciu Flavius <suciuflavius@tiscali.de>
To: linux-c-programming@vger.kernel.org
Subject: Re: different access to structs
Date: Mon, 04 Oct 2004 12:57:29 -0700 [thread overview]
Message-ID: <cjr9n9$3g1$1@sea.gmane.org> (raw)
In-Reply-To: <OF3FF607B8.2DEFEA3E-ONC1256F23.0035E913@bln.d-trust.de>
Hi,
first: NEVER but NEVER void main !!!!!!!!
main is always INT main() or simple main()
void main() is a classic Micro$oft programming poluation, the second is
the hungarian notation :D
Basicly you will use the pointer method when a group of variables share
the same readonly values.
Also you can use it to hide some struct internals :)
The second one, when every variable have his own values and you can
group together for a more logical design..
Example:
you program a RTS game, One of your entity in the world is a peasant.
All peasants have the same speed, cost the same value of gold, have the
same armor and attack, etc..
In this case, why we should retain for every peasant this attributes?
Why not just a struct Attr and every peasant struct will have a field
struct Attr *attributes ?
Image how much space do you waste if instead of a pointer (4 bytes for
example) you are replicating a struct (100 bytes let's say)
For the second case, every peasant have his own health meter, current
assigned job, etc..
In this case, we can define a struct OwnAttr and this struct must be a
member in our peasant struct :)
Hope this help.
Regards, and don't forget, never VOID main ! ;)
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) ) */
> };
>
> 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
>
next prev parent reply other threads:[~2004-10-04 19:57 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 [this message]
2004-10-04 15:00 ` Eric Bambach
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='cjr9n9$3g1$1@sea.gmane.org' \
--to=suciuflavius@tiscali.de \
--cc=linux-c-programming@vger.kernel.org \
/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).