# Passing an array of structures to a function

This program creates an array of books. We allocate space for maximum 10 books. The `sortBooksByTitle()` sorts the books by their title, using the bubble sort approach. We call that function by passing the array we created to it. Arrays are passed by reference by default.

If `strcmp(A,B)<0` it means that A and B are not in alphabetical order. That means A and B need to be swapped. However this time we are not just swapping A and B, we are swapping the entire struct Book variables that have the titles A and B. That is achieved by the `swap()` function.

Following is the complete program:

```c
#include <stdio.h>
#include <string.h>

#define MAX_TITLE 41
#define MAX_AUTHORS 3
#define MAX_NAME 20
#define MAX_BOOKS 10 // Define the maximum number of books

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;
};

void readBookDetails(struct Book *book);
void printBookDetails(struct Book book);
void sortBooksByTitle(struct Book books[], int num_books);
void swapBooks(struct Book *book1, struct Book *book2);

int main(void) {
    struct Book books[MAX_BOOKS];
    int num_books = 0;
    int continue_reading = 1;

  // Read book details until user enters 0 or reaches the maximum
   do{
    readBookDetails(&books[num_books]);
    num_books++;

    printf("\nEnter 0 to finish or any other number to continue:");
    scanf("%d",&continue_reading);
    getchar(); // Consume newline character

    if (continue_reading == 0) {
      break;
    }

  }while (num_books < MAX_BOOKS);

  // Sort books by title using bubble sort
  sortBooksByTitle(books, num_books);

  printf("\nSorted book details:\n");
  for (int i = 0; i < num_books; i++) {
    printBookDetails(books[i]);
  }

  return 0;
}
// Function to sort books by title using bubble sort
void sortBooksByTitle(struct Book books[], int num_books) {
  for (int i = 0; i < num_books - 1; i++) {
    for (int j = 0; j < num_books - i - 1; j++) {
      if (strcmp(books[j].title, books[j + 1].title) > 0) {
        swapBooks(&books[j], &books[j + 1]);
      }
    }
  }
}
// Function to swap two books in the array
void swapBooks(struct Book *book1, struct Book *book2) {
  struct Book temp = *book1;
  *book1 = *book2;
  *book2 = temp;
}
// Function to read book details from the user
void readBookDetails(struct Book *book) {
  printf("Please enter the book title.\n");
  fgets((*book).title, MAX_TITLE, stdin);
  book->title[strcspn(book->title, "\n")] = '\0'; // Remove trailing newline

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

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

    printf("  Middle initial (or hit space and enter if none): ");
    scanf("%c", &book->authors[i].middle_initial);
    getchar(); // Consume newline character

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

  printf("Please enter the price.\n");
  scanf("%f", &book->value);
}
// Function to print book details
void printBookDetails(struct Book book) {
  printf("%s \nAuthored by : ", book.title);
  // Print author names with commas
  int i;
  for (i = 0; i < book.num_authors; i++) {
    printf(" %s %c %s%s", book.authors[i].first_name,
           book.authors[i].middle_initial, book.authors[i].last_name,
           i < book.num_authors - 1 ? "," : ""); // Comma for all except last
  }
  printf("\n Price Rs. %.2f\n", book.value);
}
```

Output:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711368016001/4a77df8a-fe8a-400d-9a1c-ab9791e6f074.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711368051489/5ca4a196-1cc2-4d46-aa64-7f61823583e1.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711368110572/d8971ba0-61eb-46fe-933d-63d88851317e.png align="center")
