extracting a structure from a nested structure in c
I have following problem:
I have one global structure that has many structures inside. Now I want one of the substructures taken out and stored in some other structure.
typedef struct
{
int a;
}A;
typedef struct
{
in开发者_如何学Got b;
}B;
typedef struct
{
A dummy1;
B dummy2;
} C;
I want to declare fourth structure that extracts A from C. I did my memcpy, is it only way?
Help will be very appreciated
Thanks Huzaifa
You can assign structures. So:
typedef struct
{
A blah1;
B blah2;
/* Other members here */
} D;
C c;
D d;
...
d.blah1 = c.dummy1;
is totally fine.
Use a pointer to the struct you need:
int main() {
C c;
c.dummy1.a = 10;
c.dummy2.b = 20;
A *a;
a = &c.dummy1;
printf("%d\n", a->a);
return 0;
}
Should just be able to grab the reference of dummy1.
typedef struct { A dummy1; } D;
C var1;
D var2.dummy;
(*var2.dummy) = &var1.dummy1;
精彩评论