structures within structures 
Author Message
 structures within structures

Quote:

> if i had two structures defined as follows.
> struct struct1
> {
> int a;
> int b;
> };
> and
> struct struct2
> {
> struct struct1;
> int a;
> int b;
> };

> if i created a structure as 'struct struct2 smine;' in a program.
> how would i then put a value into the int a of the struct1 part of smine,
> and, of course, get it out?

The dot notation used to access structure member is cumulative so
to access a structure within a structure you would use something
like "s1.s2.v" where s2 is the structure within s1 and v is the
element you are accessing.  In a sense, s2.v is an element in s1.

The reason this isn't blindingly obvious to you is you have an error
in the way you are defining the second structure.  You need to give
the structure of type struct1 a name in struct2.  It should be:

struct struct2
{
  struct struct1 syours;
  int a;
  int b;

Quote:
};

And you access your element as smine.syours.a.

--

http://www.*-*-*.com/ ;              |  and a sheep voting on
+1 416 424 2871     (DoD#0082)    (eNTP)   |  what's for dinner.



Sun, 02 Apr 2000 03:00:00 GMT  
 structures within structures

Quote:

> if i had two structures defined as follows.
> struct struct1
> {
> int a;
> int b;
> };
> and
> struct struct2
> {
> struct struct1;

You need to give this struct a name.

struct struct1 x;

Quote:
> int a;
> int b;
> };

> if i created a structure as 'struct struct2 smine;' in a program.
> how would i then put a value into the int a of the struct1 part of smine,
> and, of course, get it out?

int i;

smine.x.a = 1;
i = smine.x.a;



Sun, 02 Apr 2000 03:00:00 GMT  
 structures within structures

Quote:

> if i had two structures defined as follows.
> struct struct1
> {
> int a;
> int b;
> };
> and
> struct struct2
> {
> struct struct1;
> int a;
> int b;
> };

> if i created a structure as 'struct struct2 smine;' in a program.
> how would i then put a value into the int a of the struct1 part of smine,
> and, of course, get it out?

If you added in a 'next_struct(x)' statement to both structs, you would
be on your way to creating an orthogonal linked list! (View that as a
literal BTW).

If you wanted to access and place info into int a of struct2, try this:

struct2.struct1.a = 5;

Printing it out would be the same way:

printf("Int value is %4d\n", struct2.struct1.a);

ANother thing you can do to save whitespace and make your code a bit
more readable is to do this from within the function(s) you are
accessing the structs from:

struct struct2 fubar;

fubar = struct2.struct1;

Then when you access the int of struct1, you can use:
fubar.a = 5;

This is based on code that I am using for a current working program that
works quite well.

Joe Cipale

< The opinions expressed are my own and not those of Intel >



Sun, 02 Apr 2000 03:00:00 GMT  
 structures within structures

if i had two structures defined as follows.
struct struct1
{
int a;
int b;

Quote:
};

and
struct struct2
{
struct struct1;
int a;
int b;

Quote:
};

if i created a structure as 'struct struct2 smine;' in a program.
how would i then put a value into the int a of the struct1 part of smine,
and, of course, get it out?


Sun, 02 Apr 2000 03:00:00 GMT  
 structures within structures

Quote:

> if i had two structures defined as follows.
> struct struct1
> {
> int a;
> int b;
> };
> and
> struct struct2
> {
> struct struct1;
> int a;
> int b;
> };

> if i created a structure as 'struct struct2 smine;' in a program.
> how would i then put a value into the int a of the struct1 part of smine,
> and, of course, get it out?

obviously there is the name of "struct struct1" within struct struct2
missing..
it should be something like...
struct struct2
{
struct struct1 s1;
int a;
int b;

Quote:
};

now the access to all member is...
struct2.s1.a=1;
struct2.s1.b=2;
struct2.a=3;
struct2.b=4;


Mon, 03 Apr 2000 03:00:00 GMT  
 structures within structures



...

Quote:
>obviously there is the name of "struct struct1" within struct struct2
>missing..
>it should be something like...
>struct struct2
>{
>struct struct1 s1;
>int a;
>int b;
>};

>now the access to all member is...
>struct2.s1.a=1;
>struct2.s1.b=2;
>struct2.a=3;
>struct2.b=4;

struct2 is a structure tag, not a variable name so this is incorrect.
Given the original post specified 'struct struct2 smine;' you could write:

 smine.s1.a=1;
 smine.s1.b=2;
 smine.a=3;
 smine.b=4;

--
-----------------------------------------


-----------------------------------------



Mon, 03 Apr 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Q about structures within structures

2. Initialising Structure Within Structure...How?!

3. Structue within a structure

4. how to dump easy-to-read structures from within C programs

5. How to use alias within a structure?

6. help: unnamed unions within structures

7. pointers within an array of structures

8. Sorting a last names that are contained within a structure

9. Union within structures

10. Help in initializing function pointers within structures

11. Nesting a Structure within itself?

12. Structue within a structure

 

 
Powered by phpBB® Forum Software