Link to home
Start Free TrialLog in
Avatar of stummj
stummjFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Access values of structures when the structure is nested.

If I have a nested structure such as:

typedef struct A
{  name char;
   number int;
} record_a;

typedef struct B
{ reference int;
   record_a  xxx;
} record_b;

1. What do I have to define record_a as in struc B?
2. What is the syntax to reference say the field called "name" if I am looking at struct B?

In other languages it would be something like:

return_name = record_b.record_a.name

Thats the sort of thing I'm trying to do!
Avatar of cjjclifford
cjjclifford

the "struct" for B should look like:

// use the typedef for "A"
typedef struct B {
    reference int;
    A xxx;
} record_b;


to access the info,

B my_b;
// ....
int my_number = my_b.xxx.number;

BTW, you may want to use "char *" for name, not just "char" as the latter is just a single character, and it looks like its for storing a name....

ASKER CERTIFIED SOLUTION
Avatar of cjjclifford
cjjclifford

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stummj

ASKER

SOunds good....

In struct b what datatype should I declare record_a as? (i.e. what should XXX be?)
Avatar of stummj

ASKER

Forget my dumbass last question about datatypes! record_a *is* the datatype.
And I dont even have the excuse of not enough coffee just having had my 4th :)
I'll check this out later this morning (just writing the wraparound code) and will close this question after that.

Many Thanks
J
XXX can be any identifier.Name it something indicative of what it stores.

For instance,

typedef struct B {
   int reference;
   record_a structA;
} record_b;

You can access it like:

record_b.structA.name='a';
If you are working with a pure C compiler you need to specify 'struct' and not record_a alone as data type:

typedef struct B {
   int reference;
   record_a structA;
} record_b;

must be:

typedef struct B {
   int reference;
   struct record_a structA;   /* <---- here */
} record_b;

jaime_olivares, the typedef is standard C, AFAIK, so doing "record_a xxx" is correct.

as an asside, I personally generally stay away from using typedef with strut, as it hides the nature too much (I probably got this habit from embedded programming...) whereby I would code it as:

struct A {
    int number;
    char name;
};

struct B {
    int reference;
    struct A xxx;
};

struct B my_b;
// etc

but the author used the typedef's, so I followed that suit...

cheers,
C.
All,

I've been using:

typedef struct A {
    int number;
    char name;
}A;

typedef struct B {
    int reference;
    A xxx;
}B;

for ages and I havent yet found a compiler that objects. The bonus is that you can use 'A' or 'struct A', whichever is your preference.

Paul
>>If I have a nested structure such as:
>>........
>>1. What do I have to define record_a as in struc B?
>>record_a is the type definition of A.
>>so you can declare

struct A Record_A ;
or
record_a Record_A;
both are same.

>>2. What is the syntax to reference say the field called "name" if I am looking at struct B?
>>In other languages it would be something like:
>>return_name = record_b.record_a.name
>>Thats the sort of thing I'm trying to do!

You have to create an instance

typedef struct A {  
    char *name;
    int number;
} record_a;

typedef struct B {
   int reference;
   record_a  Record_A;
} record_b;

int main(void)
{  
   record_b Record_B;
  ..........................

  printf("%d %s",Record_B.Record_A.number,Record_B.Record_A.name );

 ..............................

}
Avatar of stummj

ASKER

28 to 31 is 3 days in my book? Whatever..