1. 程式人生 > >C語言結構體 ( II )

C語言結構體 ( II )

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

結果展示: