Solved

Access values of structures when the structure is nested.

Posted on 2004-09-28
15
195 Views
Last Modified: 2010-04-15
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
Comment
Question by:stummj
15 Comments
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12167509
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
 
LVL 11

Accepted Solution

by:
cjjclifford earned 500 total points
ID: 12167517
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
 

Author Comment

by:stummj
ID: 12167555
SOunds good....

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

Author Comment

by:stummj
ID: 12167611
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
 
LVL 9

Expert Comment

by:ankuratvb
ID: 12167872
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12169018
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
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12169171
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12170048
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
 
LVL 1

Expert Comment

by:manojantony
ID: 12280387
>>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
 

Author Comment

by:stummj
ID: 12936037
28 to 31 is 3 days in my book? Whatever..
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now