# Structure with struct array as a member

```c
#include <stdio.h>

#define MAX_TITLE 41
#define MAX_AUTHORS 3
#define MAX_NAME 20

struct Author {
  char first_name[MAX_NAME];
  char middle_initial;
  char last_name[MAX_NAME];
};

struct Book {
  char title[MAX_TITLE];
  struct Author authors[MAX_AUTHORS]; // Nested Author structures
  int num_authors; // Number of authors for this book
  float value;
};

int main() {
  struct Book book1;

  printf("Please enter the book title.\n");
  fgets(book1.title, MAX_TITLE, stdin);
  book1.title[strcspn(book1.title, "\n")] = '\0';

  printf("Enter the number of authors (up to %d): ", MAX_AUTHORS);
  scanf("%d", &book1.num_authors);
  getchar(); // Consume newline character

  // Get information for each author
  for (int i = 0; i < book1.num_authors && i < MAX_AUTHORS; i++) {
    printf("Enter details for author %d:\n", i + 1);
    printf("  First name: ");
    fgets(book1.authors[i].first_name, MAX_NAME, stdin);
    book1.authors[i].first_name[strcspn(book1.authors[i].first_name, "\n")] = '\0';

    printf("  Middle initial (or '.' if none): ");
    scanf("%c", &book1.authors[i].middle_initial);
    getchar(); // Consume newline character

    printf("  Last name: ");
    fgets(book1.authors[i].last_name, MAX_NAME, stdin);
    book1.authors[i].last_name[strcspn(book1.authors[i].last_name, "\n")] = '\0';
  }

  printf("Please enter the price.\n");
  scanf("%f", &book1.value);

  printf("%s by", book1.title);
  // Print author names with commas
  for (int i = 0; i < book1.num_authors; i++) {
    printf(" %s %c. %s%s", book1.authors[i].first_name,
           book1.authors[i].middle_initial, book1.authors[i].last_name,
           i < book1.num_authors - 1 ? "," : "");
  }
  printf(" Rs. %.2f\n", book1.value);

  return 0;
}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711336094763/c911a407-59f3-430a-b868-7bcd96ec2702.png align="center")

We have to write a separate for loop to iterate through the **authors.**

Note the use of the ternary conditional operator `?:` within this `for` loop used to print the book details. We want the author names to be separated with commas but with no comma after the last author's name. So we check whether the loop variable has still not reached the last index and print a comma if its true.

### Initializing

Pay attention to how the array ***struct Author*** **authors** has been initialized with braces. Each element in that array is a ***struct Author*** variable. Notice the positional initialization and designated initialization used for **book1** and **book2** respectively.

```c
#include <stdio.h>

#define MAX_TITLE 41
#define MAX_AUTHORS 3
#define MAX_NAME 20

struct Book {
  char title[MAX_TITLE];
  int num_authors;
  struct{
      char first_name[20];
      char middle_initial;
      char last_name[20];
    }authors[MAX_AUTHORS];
  float value;
};

int main() {
    struct Book book1 = {"Berenstain Bears",
                        2,
                        {{"Stan",'M',"Berenstain"},{"Jan",'M',"Berenstain"}},
                        250};
    struct Book book2 = {.authors = {
                                        [1] = {
                                                .last_name = "Sanderson",
                                                .first_name = "Brandon"
                                                },
                                        [0] = {
                                                "Robert",.last_name = "Jordan"
                                                }
                                    },
                         .value = 450,
                         .title = "The Wheel of Time : A Memory of Light",
                         .num_authors = 2};

  printf("%s by", book1.title);
  // Print author names with commas
  int i;
  for (i=0; i < book1.num_authors; i++) {
    printf(" %s %c %s%s",
           book1.authors[i].first_name,
           book1.authors[i].middle_initial,
           book1.authors[i].last_name,
           i < book1.num_authors - 1 ? "," : ""
           );
  }
  printf("\n Price: Rs. %.2f\n", book1.value);

  printf("\n\n");

  printf("%s by", book2.title);
  // Print author names with commas
  for (i=0; i < book2.num_authors; i++) {
    printf(" %s %c %s%s",
           book2.authors[i].first_name,
           book2.authors[i].middle_initial,
           book2.authors[i].last_name,
           i < book2.num_authors - 1 ? "," : ""
           );
  }
  printf("\n Price: Rs. %.2f\n", book2.value);

  return 0;
}
```

Output:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711339550324/2bcf686b-1e8d-406d-bf1c-e1c58386a1ca.png align="center")

### Creating and initializing an array of books each with an array of authors

Self-study, Reply if any doubts.

```c
#include <stdio.h>

#define MAX_TITLE 41
#define MAX_AUTHORS 3
#define MAX_NAME 20

struct Book {
  char title[MAX_TITLE];
  int num_authors;
  struct{
      char first_name[20];
      char middle_initial;
      char last_name[20];
    }authors[MAX_AUTHORS];
  float value;
};

int main() {
    struct Book collection[] = {

                       [1] = {"Berenstain Bears",
                                2,
                                {{"Stan",'M',"Berenstain"},{"Jan",'M',"Berenstain"}},
                                250},

                       [0] = {.authors = {
                                        [1] = {
                                                .last_name = "Sanderson",
                                                .first_name = "Brandon"
                                                },
                                        [0] = {
                                                "Robert",.last_name = "Jordan"
                                                }
                                    },
                         .value = 450,
                         .title = "The Wheel of Time : A Memory of Light",
                         .num_authors = 2}
                         };

  int count = sizeof(collection)/sizeof(struct Book);
  int j;
  for (j=0;j<count;j++){
  printf("%s by", collection[j].title);
  // Print author names with commas
  int i;
  for (i=0; i < collection[j].num_authors; i++) {
    printf(" %s %c %s%s",
           collection[j].authors[i].first_name,
           collection[j].authors[i].middle_initial,
           collection[j].authors[i].last_name,
           i < collection[j].num_authors - 1 ? "," : ""
           );
  }
  printf("\n Price: Rs. %.2f\n", collection[j].value);

  printf("\n\n");
  }

  return 0;
}
```

Output:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711341014069/f11bbe66-475b-48c8-8662-ac4465805553.png align="center")
