Nick
Nick

Reputation: 213

Array of Structures in C

How would I go about changing this code into an array of structures that holds 25 records? I am familiar with structures however I am unsure as to how to declare and call an array of structures. any help would be greatly appreciated.

#include <stdio.h>

typedef struct household_t
{
    int  ID ;  
    int  Income ;  
    int  Members ; 
}  houset;

void  show_house ( houset ) ;

int main ( void) 
{
    houset  house = {1990 , 12180 , 5} ;
    show_house ( house );
    return 0 ;
}

void show_house ( houset  passed_house ) 
{
     printf ("\n\nID      Income  Members") ;
     printf ("\n\n%d      %d      %d \n\n\n", 
       passed_house.ID ,
       passed_house.Income ,
       passed_house.Members );
    return ;
}

Upvotes: 1

Views: 295

Answers (7)

Awudi Eric Okyere
Awudi Eric Okyere

Reputation: 1

create an array of the structure and pass each one or all of them if you want, into the show_house function e.g houset something[number], then you pass it to the show house function

Upvotes: 0

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76405

What you want to do, is create an array, like any other array:

houset hose_array[25] = {
     {1990 , 12180 , 5},
     {1234 , 56789 , 0}
};//initialize here, if you want to

But you'll have to change your show_house function, as arrays decay into pointers when you pass them to functions (most of the time). Perhaps change it to this:

void show_houses(houset *houses, int len)
{
    puts("\n\nID      Income  Members\n") ;
    while(len)
    {
        printf ("%d      %d      %d \n", 
               passed_house->ID ,//indirection operators!
               passed_house->Income ,
               passed_house->Members );
               passed_house++;//move pointer to next house
        --len;
    }
}

This will print out all the houses, specified by len. see it in action

Stack Overflow:
Mind you, you may find that an array like this might deplete the stack memory. In which case you'll find yourself working with either an array of pointers to structs, or pointers to pointers. If you're not familiar with dynamic memory in C, I'd suggest you read up on that, first...
If you know how it works, more or less:

houset *house[25];//an array of 25 pointers:
house[0] = malloc(sizeof(houset));
//we're dealing with a pointer, and need the indirection operator here, too
house[0]->ID = 1990;
//the alternative is less readable, IMO:
(*house[0]).Income = 12180;//deref pointer, then direct access is poissible
house[0]->Members = 5;
house[1] = malloc(sizeof(houset));
house[1]->ID = 1991;
house[1]->Income = 22180;
house[1]->Members = 3;

But now, as I said: arrays decay into pointers, so we'll have to change our show_houses function once more, too. This is where it becomes a bit tricky:

void show_houses(houset **houses, int len)
{//pointer to pointer!
    puts("ID      Income  Members\n") ;
    while(len)
    {//dereference the first pointer
        printf ("%d      %d      %d \n", 
                (*passed_house)->ID ,//second pointer, still requires indirection 
                (*passed_house)->Income ,
                (*passed_house)->Members );
        passed_house++;//shift pointer to pointer by 1
        --len;
    }
}

Don't forget to free the memory, once you're done with it:

free(house[0]);
free(house[1]);

to avoid mem-leaks.
Once again, this codepad can serve as a working example

Upvotes: 3

haccks
haccks

Reputation: 106012

You can declare an array of structures as

houset  house[25];  

and call it from your main function as

  show_house ( house[i] );

Upvotes: 2

Vishal R
Vishal R

Reputation: 1074

Please have a look at the code below. I hope the concept is simple and easy to understand. If you face any difficulty post a comment.

#include <stdio.h>

typedef struct household_t
{
    int  ID ;
    int  Income ;
    int  Members ;
}  houset;

void  show_house ( houset[],int ) ;

int main ( void)
{
    houset  house[2] = {{1990 , 12180 , 5},{1220,12211,3}} ;
    show_house ( house, 2); //size of array of struct
    return 0 ;
}

void show_house ( houset  passed_house[], int size )
{
int i=0;
 for (i=0;i<size;i++)
{
     printf ("\n\nID      Income  Members") ;
     printf ("\n\n%d      %d      %d \n\n\n",
       passed_house[i].ID ,
       passed_house[i].Income ,
       passed_house[i].Members );
}
    return ;
}

Upvotes: 1

Wald
Wald

Reputation: 1091

I suppose your are asking for something like

household_t house_info[size*];

house_info[1] = {1990 , 12180 , 5};

Upvotes: 1

Tam&#225;s Zahola
Tam&#225;s Zahola

Reputation: 9321

houset houses[25];

And using as function argument:

void show_houses(houset * houses, int nHouses){

for(int i = 0; i < nHouses; i++){
show_house(houses[i]);
}

}

...

show_houses(houses, 25);

Upvotes: 1

JIghtuse
JIghtuse

Reputation: 876

It is almost as simple as with usual C types:

int main ( void) 
{
    houset houses[25];
    int i;
    for (i = 0; i < 25; i++) {
        houses[i].ID = 1990;
        houses[i].Income = 12180;
        houses[i].Members = 5;
        show_house (houses[i]);
    }
    return 0 ;
}

You also can initialize it as you do with one structure:

houset houses[25] = {
    { 1990, 12180, 5 },
    { 1991, 12178, 6 },
    /* ... */
}

If you initialize less then 25 structures others will be filled with zeros.

Upvotes: 2

Related Questions