And last but not least – arrays of structs.

Of course there is one final thing – creating arrays of struct. Most often, a struct will not exist in isolation, you may require a multitude of them. For example, a book struct is useful, but an array of them is even more constructive – this makes a library. There are of course a number of ways of doing this.

1. Using a simple array of struct

So if we use this struct:

struct bookinfo {
    char title[50];
    char author[50];
    char subject[100];
    char isbn[11];
    int year;
};

we can then create an array of struct where it is needed, for example main():

struct bookinfo cooking[100];

This creates an array, cooking with 100 elements of type bookinfo. Individual members of each element can now be assigned in the following manner:

strcpy(cooking[0].title, "Fun with Foie Gras");
cooking[0].year = 2015;

Function parameters can also be modified to allow for the use of this array of structs:

void enterBookdata(struct bookinfo b[], int index)
{
    printf("Book title? ");
    fgets(b[index].title, 50, stdin);
    printf("Book author? ");
    fgets(b[index].author, 50, stdin);
    printf("Book subject? ");
    fgets(b[index].subject, 100, stdin);
    printf("Book ISBN? ");
    fgets(b[index].isbn, 11, stdin);
    printf("Year of publication? ");
    scanf("%d", &b[index].year);
}

which could be called in the following manner (from main()):

enterBookdata(cooking,0);

2. Using an array created using a typedef

The second approach may be more convenient, especially when it comes to creating function parameters. Using the same struct as above, then a typedef which is an array of struct can be defined:

typedef struct bookinfo library[100];

Now in main(), a variable can be defined:

 library cooking;

The function will now look like this:

void enterBookdata(library b, int index)
{
    printf("Book title? ");
    fgets(b[index].title, 50, stdin);
    printf("Book author? ");
    fgets(b[index].author, 50, stdin);
    printf("Book subject? ");
    fgets(b[index].subject, 100, stdin);
    printf("Book ISBN? ");
    fgets(b[index].isbn, 11, stdin);
    printf("Year of publication? ");
    scanf("%d", &b[index].year);
}

which could be called in the following manner (from main()):

enterBookdata(cooking,0);

Note that because BOTH approaches use arrays, there is no necessity to define a pass-by-reference parameter in the function  in order to modify the contents of each of the structs in the array.

If the struct created contains dynamic members, then of course the complexity of what is to be done will increase.

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s