Solved

Access values of structures when the structure is nested.

Posted on 2004-09-28
15
197 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

809 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