* different access to structs
@ 2004-10-04 10:15 p.boehm
2004-10-04 19:57 ` Suciu Flavius
0 siblings, 1 reply; 3+ messages in thread
From: p.boehm @ 2004-10-04 10:15 UTC (permalink / raw)
To: linux-c-programming
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: different access to structs
2004-10-04 19:57 ` Suciu Flavius
@ 2004-10-04 15:00 ` Eric Bambach
0 siblings, 0 replies; 3+ messages in thread
From: Eric Bambach @ 2004-10-04 15:00 UTC (permalink / raw)
To: Suciu Flavius; +Cc: linux-c-programming
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: different access to structs
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
0 siblings, 1 reply; 3+ messages in thread
From: Suciu Flavius @ 2004-10-04 19:57 UTC (permalink / raw)
To: linux-c-programming
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
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2004-10-04 19:57 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 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).