This program defines a struct Books that represents a book, with members for the book title, author, subject, and ID. It then defines a function printBook that takes a pointer to a Books struct and prints out the book's details using the arrow operator -> to access the struct members.
In main(), two instances of the Books struct are created, with their details initialized using strcpy and = operators. The function printBook is then called twice, passing in the addresses of Book1 and Book2 respectively. The printBook function dereferences the pointer to the Books struct using the arrow operator ->, and then prints out each of the struct members.
This program demonstrates how to use pointers to pass struct instances as arguments to functions, and how to use the arrow operator to access the members of the struct via a pointer. It also shows how to use strcpy to copy strings into the char arrays that make up the struct members.
#include<iostream> #include<cstring> using namespace std; void printBook(struct Books *book); struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; int main() { struct Books Book1; struct Books Book2; strcpy(Book1.title,"Mathematics for IITJEE"); strcpy(Book1.author,"RD Sharma"); strcpy(Book1.subject,"JEE Mathematics"); Book1.book_id = 6495407; strcpy( Book2.title,"Physics"); strcpy( Book2.author,"IE Irodov"); strcpy( Book2.subject,"JEE Mechanical Physics"); Book2.book_id=6495700; printBook( &Book1 ); printBook( &Book2 ); return 0; } void printBook(struct Books *book) { cout<<"Book title : "<<book->title <<endl; cout<<"Book author : "<<book->author <<endl; cout<<"Book subject : "<<book->subject <<endl; cout<<"Book id : "<<book->book_id <<endl; }To download raw file Click Here
Book title : Mathematics for IITJEE Book author : RD Sharma Book subject : JEE Mathematics Book id : 6495407 Book title : Physics Book author : IE Irodov Book subject : JEE Mechanical Physics Book id : 6495700
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions