C++ language introduction tutorial -17 structure

1 struct Books {char title[50]; char author[50]; char subject[100]; int book_id; } book;

Struct {int a; char b; double c; } S;

S s;

// We can treat S as a type, like an int, so S S; That’s a statement

S.A.S.B.C. These can be used as normal variables.

We can also declare the array S S [5];

s[0]. a

s[0].b

s[0].c

Example:

#include <iostream>
#include <string.h>
using namespace std;
 
struct Books
{
   char  title[50];
   char  author[50];
   int   book_id;
} book;

int main (a)
{
   gets(book.title); 
   gets(book.author); 
   scanf("%d",&book.book_id);
   
   cout<<"Title:"<<book.title<<endl;
   cout<<"Author:"<<book.author<<endl;
  cout<<"Book No. :"<<book.book_id<<endl;
    
   return 0;
}
Copy the code

Running results:



In the example above, we can see that we compare the book to a whole, and then the book has the title, author, book number and so on.

In the future, things like this can be represented in terms of structures.