4,访问结构体成员:
我们使用成员访问运算符(.)进行访问
//结构体练习
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
struct Books {
char tittle[50];
char author[50];
char subject[100];
int book_id;
};
int main()
{
struct Books book1;
struct Books book2;
strcpy(book1.tittle,"概率论与数理统计");
strcpy(book1.author,"张六六") ;
strcpy(book1.subject,"概论科学与技术");
book1.book_id=121212;
strcpy(book2.tittle,"计算机科学与技术");
strcpy(book2.author,"张八八") ;
strcpy(book2.subject,"交叉信息科学研究");
book2.book_id=251212;
cout<<book1.tittle<<endl;
cout<<book1.author<<endl;
cout<<book1.subject<<endl;
cout<<book1.book_id<<endl;
cout<<endl;
cout<<book2.tittle<<endl;
cout<<book2.author<<endl;
cout<<book2.subject<<endl;
cout<<book2.book_id<<endl;
return 0;
}
代码展示:
//结构体练习
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
struct Books {
char tittle[50];
char author[50];
char subject[100];
int book_id;
};
int main()
{
struct Books book1;
struct Books book2;
strcpy(book1.tittle,"概率论与数理统计");
strcpy(book1.author,"张六六") ;
strcpy(book1.subject,"概论科学与技术");
book1.book_id=121212;
strcpy(book2.tittle,"计算机科学与技术");
strcpy(book2.author,"张八八") ;
strcpy(book2.subject,"交叉信息科学研究");
book2.book_id=251212;
cout<<book1.tittle<<endl;
cout<<book1.author<<endl;
cout<<book1.subject<<endl;
cout<<book1.book_id<<endl;
cout<<endl;
cout<<book2.tittle<<endl;
cout<<book2.author<<endl;
cout<<book2.subject<<endl;
cout<<book2.book_id<<endl;
return 0;
}
结果展示: