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

Discussion in 'C Programming' started by stuie_norris@yahoo.com.au, Dec 12, 2012.

  1. Guest

    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;
    }
    , Dec 12, 2012
    #1
    1. Advertising

  2. Re: Iterate through a list of structure arrays of structure to getout the field

    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.
    Johann Klammer, Dec 12, 2012
    #2
    1. Advertising

  3. Rui Maciel Guest

    Re: Iterate through a list of structure arrays of structure to get out the field

    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
    Rui Maciel, Dec 12, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gogo
    Replies:
    1
    Views:
    2,082
    Sudsy
    Sep 4, 2003
  2. runescience
    Replies:
    0
    Views:
    1,437
    runescience
    Feb 9, 2006
  3. David Pratt

    Iterate through a list calling functions

    David Pratt, Jun 5, 2005, in forum: Python
    Replies:
    3
    Views:
    423
    Kent Johnson
    Jun 6, 2005
  4. John
    Replies:
    4
    Views:
    892
    RedGrittyBrick
    Apr 1, 2008
  5. Sound
    Replies:
    2
    Views:
    426
    Randy Webb
    Sep 28, 2006
Loading...

Share This Page