Latest web development tutorials

C ++ data structures

C / C ++ allows you to define the array variable to store the same type of data items, but thestructure is C ++, another user-defined data types available, which allows you to store different types of data items.

Structure used to represent a record, suppose you want to track dynamic library books, you may need to keep track of each book the following properties:

  • Title
  • Author
  • Subject
  • Book ID

Definition structure

In order to define the structure, you must use thestruct statement.struct statement defines a new data type that contains a plurality of members, the format struct statement is as follows:

struct [structure tag]
{
   member definition;
   member definition;
   ...
   member definition;
} [one or more structure variables];  

structure tag is optional, every member definition is the standard definition of a variable, such as int i; or float f; or other valid variable definitions.At the end of the structure defined, the last one before the semicolon, you can specify one or more structural variables, which is optional. Here is the structure declaration Book by:

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

Access to structure member

In order to access members of the structure, we usethe member access operator (.).Member access operator is a period structure variable names and structures of our members want to access between. You can define the type of a variable structure usingthe struct keyword.The following example demonstrates the use of the structure:

#include <iostream>
#include <cstring>
 
using namespace std;
 
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main( )
{
   struct Books Book1;        // 声明 Book1,类型为 Book
   struct Books Book2;        // 声明 Book2,类型为 Book
 
   // Book1 详述
   strcpy( Book1.title, "Learn C++ Programming");
   strcpy( Book1.author, "Chand Miyan"); 
   strcpy( Book1.subject, "C++ Programming");
   Book1.book_id = 6495407;

   // Book2 详述
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Yakit Singha");
   strcpy( Book2.subject, "Telecom");
   Book2.book_id = 6495700;
 
   // 输出 Book1 信息
   cout << "Book 1 title : " << Book1.title <<endl;
   cout << "Book 1 author : " << Book1.author <<endl;
   cout << "Book 1 subject : " << Book1.subject <<endl;
   cout << "Book 1 id : " << Book1.book_id <<endl;

   // 输出 Book2 信息
   cout << "Book 2 title : " << Book2.title <<endl;
   cout << "Book 2 author : " << Book2.author <<endl;
   cout << "Book 2 subject : " << Book2.subject <<endl;
   cout << "Book 2 id : " << Book2.book_id <<endl;

   return 0;
}

When the above code is compiled and executed, it produces the following results:

Book 1 title : Learn C++ Programming
Book 1 author : Chand Miyan
Book 1 subject : C++ Programming
Book 1 id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Yakit Singha
Book 2 subject : Telecom
Book 2 id : 6495700

Structure as a function parameter

You can put the structure as a function parameter, the Senate manner similar to other types of variables or pointers. You can use the above example the way to access structure variables:

#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;        // 声明 Book1,类型为 Book
   struct Books Book2;        // 声明 Book2,类型为 Book
 
   // Book1 详述
   strcpy( Book1.title, "Learn C++ Programming");
   strcpy( Book1.author, "Chand Miyan"); 
   strcpy( Book1.subject, "C++ Programming");
   Book1.book_id = 6495407;

   // Book2 详述
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Yakit Singha");
   strcpy( Book2.subject, "Telecom");
   Book2.book_id = 6495700;
 
   // 输出 Book1 信息
   printBook( Book1 );

   // 输出 Book2 信息
   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;
}

When the above code is compiled and executed, it produces the following results:

Book title : Learn C++ Programming
Book author : Chand Miyan
Book subject : C++ Programming
Book id : 6495407
Book title : Telecom Billing
Book author : Yakit Singha
Book subject : Telecom
Book id : 6495700

Pointer to a structure

You can define a pointer to the structure, the way the definition of links to other similar types of pointer variable, as follows:

struct Books *struct_pointer;

Now you can address storage structure variables in the above definition of a pointer variable. To find the address of the structure variable, the & operator in front of the name of the structure, as follows:

struct_pointer = &Book1;

To use the pointer to access the member structure points to the structure, you must use the -> operator, as follows:

struct_pointer->title;

Let's use the structure pointer to rewrite the above example, it will help you understand the concept of structure pointers:

#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;        // 声明 Book1,类型为 Book
   struct Books Book2;        // 声明 Book2,类型为 Book */
 
   // Book1 详述
   strcpy( Book1.title, "Learn C++ Programming");
   strcpy( Book1.author, "Chand Miyan"); 
   strcpy( Book1.subject, "C++ Programming");
   Book1.book_id = 6495407;

   // Book2 详述
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Yakit Singha");
   strcpy( Book2.subject, "Telecom");
   Book2.book_id = 6495700;
 
   // 通过传 Book1 的地址来输出 Book1 信息
   printBook( &Book1 );

   // 通过传 Book2 的地址来输出 Book2 信息
   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;
}

When the above code is compiled and executed, it produces the following results:

Book title : Learn C++ Programming
Book author : Chand Miyan
Book subject : C++ Programming
Book id : 6495407
Book title : Telecom Billing
Book author : Yakit Singha
Book subject : Telecom
Book id : 6495700

typedef keyword

Here is a simpler way to define the structure, you can create the type to take a "Alias." E.g:

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

Now you can use to define variablesBooksBooks types, without the need to use the struct keyword. The following are examples:

Books Book1, Book2;

You can use thetypedef keyword to define non-structural type, as follows:

typedef long int *pint32;
 
pint32 x, y, z;

x, y and z are pointing to a long integer long int pointer.