Define structure. Explain nested structure with example. Create a structure named book with nameauthor, and publisher as its members. Write a program using this structure to read data of 50 books and display name of those books published by “XYX” publisher.

This answer is restricted. Please login to view the answer of this question.

Login Now

A structure is a collection of variables under a single name. These variables can be of different types, and each has a name that is used to select it from the structure. The variables are called members of the structure. A structure is a convenient way of grouping general pieces of the related information together.

A structure can be defined as a new named type or user-defined data type, thus extending the number of available types. It can be our other structures, arrays, or pointers as some of its members.

The syntax of structure is

struct structure_name
{
    data_type member_variables1;
    data_type member_variables2;
    ...... .......
    data_type member_variablesn;
}

Once structure_name is declared as a new data type, the variable of that can be declared as

struct structure_name structure_variable;

Program Part:

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

#define SIZE 5

struct book
{
    char name[100];
    char author[100];
    char publisher[100];
};

int main(){

    int i;
    struct book b[SIZE];

    printf("Enter %d book data: \n", SIZE);
    for(i = 0; i < SIZE; i++){
        printf("\nEnter %d book Details:\nName: ", i + 1);
        gets(b[i].name);
        printf("Author: ");
        gets(b[i].author);
        printf("Publisher: ");
        gets(b[i].publisher);
    }

    printf("\nBook Published by XYZ are:\n");
    for(i = 0; i < SIZE; i++){
        if(strcmp(b[i].publisher, "XYZ") == 0){
            printf("%s\n", b[i].name);
        }
    }

    return 0;
}

 

If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .