Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Iterate through a list of structure arrays of structure to get outthe field

Reply
Thread Tools

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

 
 
stuie_norris@yahoo.com.au
Guest
Posts: n/a
 
      12-12-2012
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;
}
 
Reply With Quote
 
 
 
 
Johann Klammer
Guest
Posts: n/a
 
      12-12-2012
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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.
 
Reply With Quote
 
 
 
 
Rui Maciel
Guest
Posts: n/a
 
      12-12-2012
(E-Mail Removed) wrote:

> 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>
rui@kubuntu: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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to iterate 2 nested collections w <logic:iterate> without a"getter" John Java 4 04-01-2008 09:46 AM
1.Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Sound Javascript 2 09-28-2006 02:43 PM
nested:iterate or logic: iterate with multibox?? runescience Java 0 02-09-2006 12:57 AM
Iterate through a list calling functions David Pratt Python 3 06-06-2005 02:07 AM
<logic:iterate /> iterate beyond items in the collection Gogo Java 1 09-04-2003 08:40 PM



Advertisments