Solved

Access values of structures when the structure is nested.

Posted on 2004-09-28
15
198 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Concatenate two strings Last and First Name 10 60
Problem to scan all sheets 3 111
How to creat good software interface ? 1 68
Raspberry Pi 3 to send text message 9 151
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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

828 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