C++指向结构体的指针

#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;        // Declare Book1 of type Book
   struct Books Book2;        // Declare Book2 of type Book

   // Book 1 specification
   strcpy( Book1.title, Learn C++ Programming);
   strcpy( Book1.author, Chand Miyan); 
   strcpy( Book1.subject, C++ Programming);
   Book1.book_id = 6495407;

   // Book 2 specification
   strcpy( Book2.title, Telecom Billing);
   strcpy( Book2.author, Yakit Singha);
   strcpy( Book2.subject, Telecom);
   Book2.book_id = 6495700;

   // Print Book1 info, passing address of structure
   printBook( &Book1 );

   // Print Book1 info, passing address of structure
   printBook( &Book2 );

   return 0;
}

// This function accept pointer to structure as parameter.
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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


#include <iostream> using namespace std; int main () { int*ptr = NULL; cout << "The value of ptr is " << ptr ;
#include <iostream> using namespace std; int main () { intvar = 20;// actual variable declaration.
// #include <iostream> using namespace std; int main () { intvar1; char var2[10]; cout << "Address of var1 variable: ";
#include <iostream> #include <string> using namespace std; int main () { string str1 = "Hello";
#include <iostream> #include <cstring> using namespace std; int main () { char str1[10] = "Hello";
#include <iostream> using namespace std; int main () { char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\\0'};
#include <iostream> #include <ctime> using namespace std; // function to generate and retrun random numbers.
#include <iostream> using namespace std; double getAverage(int arr[], int size) { int i, sum = 0; double avg;
#include <iostream> using namespace std; int main () { // an array with 5 elements. double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
#include <iostream> using namespace std; int main () { // an array with 5 rows and 2 columns. int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
#include <iostream> using namespace std; #include <iomanip> using std::setw; int main () { int n[ 10 ];// n is an array of 10 integers
#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main () {
#include <iostream> #include <cmath> using namespace std; int main () { // number definition:
#include <iostream> using namespace std; int main () { // number definition: shorts; inti; longl; floatf;
#include <iostream> using namespace std; // function definition to swap the values. void swap(int &x, int &y) {
#include <iostream> using namespace std; // function definition to swap the values. void swap(int *x, int *y) {
#include <iostream> using namespace std; void swap(int x, int y) { int temp; temp = x; /* save the value of x */
#include <iostream> using namespace std; int sum(int a, int b = 20) { int result; result = a + b; return (result);
// 标准输入流 #include <iostream> using namespace std; int main() { char name[50]; cout << "Please enter your name: ";
// C++标准输出流 #include <iostream> using namespace std; int main() { char str[] = "Hello C++";