Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

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!
0
stummj
Asked:
stummj
1 Solution
 
cjjcliffordCommented:
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....

0
 
cjjcliffordCommented:
sorry, ignore my bit about redefining the "typedef struct B"... I'm still working my way through my first coffee....

BUT, you have to rewrite to place the types before the identifiers....

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

typedef struct B {
    int reference;
    record_a xxx;
} record_b;
0
 
stummjAuthor Commented:
SOunds good....

In struct b what datatype should I declare record_a as? (i.e. what should XXX be?)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
stummjAuthor Commented:
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
0
 
ankuratvbCommented:
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';
0
 
Jaime OlivaresCommented:
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;

0
 
cjjcliffordCommented:
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.
0
 
PaulCaswellCommented:
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
0
 
manojantonyCommented:
>>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 );

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

}
0
 
stummjAuthor Commented:
28 to 31 is 3 days in my book? Whatever..
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now