Iterate through a list of structure arrays of structure to get outthe field


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;
}
 
Ad

Advertisements

J

Johann Klammer

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 think you need an inner loop(inside the for loop) to iterate thru the
i->hf[] elements array. You might want to store the number of elements
somewhere(in the ListItem structure?) or terminate the hf_1/hf_2 arrays
with some kind of sentinel value. Otherwise it will be problematic
getting a termination condition.
 
Ad

Advertisements

R

Rui Maciel

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.

You can't expect your code to do something else than what you told it to do.
See bellow:

#include <stdio.h>

struct header_field_info {
const char *name;
int num1;
};

typedef struct info {
header_field_info hfinfo;

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

So you declared two objects of type struct ListItem: h1, and h2.

h1.hf = hf_1;

h1.hf points to hf_1[0]
h1.next =&h2;

h1.next points to object h2.

h2.hf = hf_2;

h2.hf points to hf_2[0]
h2.next = NULL;

....and h2.next points to NULL.

That is, you've defined a linked list with exactly two entries: h1 and h2.

for (ListItem *i = &h1; i != NULL; i = i->next) {

for (struct ListItem *i = &h1; i != NULL; i = i->next) {

In this loop, you iterate through the linked list you've just defined, the
one which is essentially hf_1[0] ==> hf_2[0] ==> NULL.

int sizeh1 = sizeof ((*i).hf);

size_t sizeh1 = sizeof ((*i).hf);

printf ("Size of hf is %d\n", sizeh1);
printf ("%d\n", (*i->hf).hfinfo.num1);
}
return 0;
}


<output>
[email protected]:tmp$ ./a.out
Size of hf is 8
1
Size of hf is 8
4
</output>

As should be expected, because that's exactly what you told it to do.

In addition, arrays are a poor choice to iterate over if you failed to
provide any information on their size. So, either hf_1 and hf_2 share a
fixed length which you know at compile time, or you need to find a different
data structure to store your Info objects.


Rui Maciel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top