S
stuie_norris
Hi Forum,
I am trying to iterate through a list of structure arrays of structure to get out the field. I want to process the fields of the two structures.
In the code below I am only getting the first element of each of the arrays of structures that I am inserting into my list.
What have I done wrong that is stopping me from get the num field of each of the elements of the two arrays as I iterate through the list of strucutes.
Thanks
Stuie
#include <stdio.h>
struct header_field_info {
const char *name;
int num1;
};
typedef struct info {
header_field_info hfinfo;
} Info;
Info hf_1[] = {{"Item 1", 1}, { "Item 2", 2}, { "Item 3", 3}};
Info hf_2[] = {{"Item 4", 4}, { "Item 5", 5}};
struct ListItem {
Info *hf;
struct ListItem * next;
};
int main(int argc, char ** argv) {
struct ListItem h1;
struct ListItem h2;
h1.hf = hf_1;
h1.next =&h2;
h2.hf = hf_2;
h2.next = NULL;
for (ListItem *i = &h1; i != NULL; i = i->next) {
int sizeh1 = sizeof ((*i).hf);
printf ("Size of hf is %d\n", sizeh1);
printf ("%d\n", (*i->hf).hfinfo.num1);
}
return 0;
}
I am trying to iterate through a list of structure arrays of structure to get out the field. I want to process the fields of the two structures.
In the code below I am only getting the first element of each of the arrays of structures that I am inserting into my list.
What have I done wrong that is stopping me from get the num field of each of the elements of the two arrays as I iterate through the list of strucutes.
Thanks
Stuie
#include <stdio.h>
struct header_field_info {
const char *name;
int num1;
};
typedef struct info {
header_field_info hfinfo;
} Info;
Info hf_1[] = {{"Item 1", 1}, { "Item 2", 2}, { "Item 3", 3}};
Info hf_2[] = {{"Item 4", 4}, { "Item 5", 5}};
struct ListItem {
Info *hf;
struct ListItem * next;
};
int main(int argc, char ** argv) {
struct ListItem h1;
struct ListItem h2;
h1.hf = hf_1;
h1.next =&h2;
h2.hf = hf_2;
h2.next = NULL;
for (ListItem *i = &h1; i != NULL; i = i->next) {
int sizeh1 = sizeof ((*i).hf);
printf ("Size of hf is %d\n", sizeh1);
printf ("%d\n", (*i->hf).hfinfo.num1);
}
return 0;
}