Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Structures (struct)

C++ Structures (struct)

C/C++ arrays allow defining variables that store data items of the same type, butstructuresare another user-defined data type available in C++ that allow you to store data items of different types.

A structure is used to represent a record. Suppose you want to track the dynamics of books in a library; you may need to track the following attributes of each book:

Defining a Structure

In C++, the struct statement is used to define a structure.

A structure is a user-defined data type used to combine different types of data together. Similar to a class, a structure allows you to define member variables and member functions.

To define a structure, you must use the struct statement. The struct statement defines a new data type containing multiple members; the format of the struct statement is as follows:

struct

type_name

{

member_type1

member_name1

;

member_type2

member_name2

;

member_type3

member_name3

; . .

}

object_names

;

type_name is the name of the structure type,member_type1 member_name1 is a standard variable definition, such as int i; or float f; or other valid variable definitions. At the end of the structure definition, before the final semicolon, you can specify one or more structure variables; this is optional. The following declares a structure type Books, with variables book:

struct

Books

{

char

title

[

50

]

;

char

author

[

50

]

;

char

subject

[

100

]

;

int

book_id

;

}

book

;

Advantages of structures:

Accessing Structure Members

To access the members of a structure, we use themember access operator (.). The member access operator is a period between the structure variable name and the structure member we want to access.

The example below demonstrates the usage of structures:

Example

#include

<
iostream
#include
<
cstring
using
namespace
std

;

// Declare a structure type Books

struct

Books

{

char

title

[

50

]

;

char

author

[

50

]

;

char

subject

[

100

]

;

int

book_id

;

}

;

int

main

(

)

{

Books

Book1

;

// Define a variable Book1 of structure type Books

Books

Book2

;

// Define a variable Book2 of structure type Books

// Book1 details

strcpy

(

Book1

.

title

,

"

C++ Tutorial

"

)

;

strcpy

(

Book1

.

author

,

"

Runoob

"

)

;

strcpy

(

Book1

.

subject

,

"

Programming Language

"

)

;

Book1

.

book_id

=

12345

;

// Book2 details

strcpy

(

Book2

.

title

,

"

CSS Tutorial

"

)

;

strcpy

(

Book2

.

author

,

"

Runoob

"

)

;

strcpy

(

Book2

.

subject

,

"

Front-end Technology

"

)

;

Book2

.

book_id

=

12346

;

// Output Book1 information

cout

<<

"

First book title:

"

<<

Book1

.

title

<<

endl

;

cout

<<

"

First book author:

"

<<

Book1

.

author

<<

endl

;

cout

<<

"

First book category:

"

<<

Book1

.

subject

<<

endl

;

cout

<<

"

First book ID:

"

<<

Book1

.

book_id

<<

endl

;

// Output Book2 information

cout

<<

"

Second book title:

"

<<

Book2

.

title

<<

endl

;

cout

<<

"

Second book author:

"

<<

Book2

.

author

<<

endl

;

cout

<<

"

Second book category:

"

<<

Book2

.

subject

<<

endl

;

cout

<<

"

Second book ID:

"

<<

Book2

.

book_id

<<

endl

;

return

0

;

}

The example defines a structure type Books and its two variables Book1 and Book2. When the code above is compiled and executed, it produces the following result:


第一本书标题 : C++ 教程
第一本书作者 : Runoob
第一本书类目 : 编程语言
第一本书 ID : 12345
第二本书标题 : CSS 教程
第二本书作者 : Runoob
第二本书类目 : 前端技术
第二本书 ID : 12346

Structures as Function Parameters

You can use a structure as a function parameter; the way of passing parameters is similar to other types of variables or pointers. You can use the approach in the example above to access structure variables:

Example

#include

<
iostream
#include
<
cstring
using
namespace
std

;

void

printBook

(

struct

Books

book

)

;

// Declare a structure type Books

struct

Books

{

char

title

[

50

]

;

char

author

[

50

]

;

char

subject

[

100

]

;

int

book_id

;

}

;

int

main

(

)

{

Books

Book1

;

// Define a variable Book1 of structure type Books

Books

Book2

;

// Define a variable Book2 of structure type Books

// Book1 details

strcpy

(

Book1

.

title

,

"

C++ Tutorial

"

)

;

strcpy

(

Book1

.

author

,

"

Runoob

"

)

;

strcpy

(

Book1

.

subject

,

"

Programming Language

"

)

;

Book1

.

book_id

=

12345

;

// Book2 details

strcpy

(

Book2

.

title

,

"

CSS Tutorial

"

)

;

strcpy

(

Book2

.

author

,

"

Runoob

"

)

;

strcpy

(

Book2

.

subject

,

"

Front-end Technology

"

)

;

Book2

.

book_id

=

12346

;

// Output Book1 information

printBook

(

Book1

)

;

// Output Book2 information

printBook

(

Book2

)

;

return

0

;

}

void

printBook

(

struct

Books

book

)

{

cout

<<

"

Book title:

"

<<

book

.

title

<<

endl

;

cout

<<

"

Book author:

"

<<

book

.

author

<<

endl

;

cout

<<

"

Book category:

"

<<

book

.

subject

<<

endl

;

cout

<<

"

Book ID:

"

<<

book

.

book_id

<<

endl

;

}

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


书标题 : C++ 教程
书作者 : Runoob
书类目 : 编程语言
书 ID : 12345
书标题 : CSS 教程
书作者 : Runoob
书类目 : 前端技术
书 ID : 12346

Detailed Introduction to Each Part of a Structure

Pointers to Structures

You can define a pointer to a structure in a similar way to defining a pointer to variables of other types, as shown below:


struct Books *struct_pointer;

The code above defines a pointer struct_pointer that points to the Books structure.

Now, you can store the address of a structure variable in the pointer variable defined above. To find the address of a structure variable, place the & operator before the structure name, as shown below:


struct_pointer = &Book1;

The code above assigns the address of the Book1 structure variable to struct_pointer.

To access the members of the structure using a pointer to it, you must use the -> operator, as shown below:


struct_pointer->title;

The code above accesses the title member of the Book1 structure through struct_pointer.

Let us rewrite the example above using structure pointers; this will help you understand the concept of structure pointers:

Example

#include

<
iostream
#include
<
string
using
namespace
std

;

// Declare a structure type Books

struct

Books

{

string

title

;

string

author

;

string

subject

;

int

book_id

;

// Constructor

Books

(

string

t

,

string

a

,

string

s

,

int

id

)

:

title

(

t

)

,

author

(

a

)

,

subject

(

s

)

,

book_id

(

id

)

{

}

}

;

// Function to print book information, accepts a pointer to the Books structure

void

printBookInfo

(

const

Books

book

)

{

cout

<<

"

Book title:

"

<<

book

->

title

<<

endl

;

cout

<<

"

Book author:

"

<<

book

->

author

<<

endl

;

cout

<<

"

Book category:

"

<<

book

->

subject

<<

endl

;

cout

<<

"

Book ID:

"

<<

book

->

book_id

<<

endl

;

}

int

main

(

)

{

// Create objects for two books

Books

Book1

(

"

C++ Tutorial

"

,

"

Runoob

"

,

"

Programming Language

"

,

12345

)

;

Books

Book2

(

"

CSS Tutorial

"

,

"

Runoob

"

,

"

Front-end Technology

"

,

12346

)

;

// Use pointers to point to the objects of these two books

Books

ptrBook1

= &

Book1

;

Books

ptrBook2

= &

Book2

;

// Output book information, passing the pointer

printBookInfo

(

ptrBook1

)

;

printBookInfo

(

ptrBook2

)

;

return

0

;

}

Explanation:

-

Created two Books objects of type Book1 and Book2.

-

Use & operator to get the addresses of these two objects and assign them to the pointer ptrBook1 and ptrBook2.

-

called printBookInfo function, we pass pointers to the Books objects.

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


书籍标题: C++ 教程
书籍作者: Runoob
书籍类目: 编程语言
书籍 ID: 12345
书籍标题: CSS 教程
书籍作者: Runoob
书籍类目: 前端技术
书籍 ID: 12346

typedef Keyword

The following is a simpler way to define a structure; you can give an "alias" to the type you create. For example:


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

Now, you can directly use Books to define Books type variables without using the struct keyword. The following is an example:


Books Book1, Book2;

You can use the typedef keyword to define non-structure types, as shown below:


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

x, y, and z are all pointers to the long int type.


Differences Between Structures and Classes

In C++, struct and class are essentially very similar; the only difference is the default access permission:

You can think of struct as a simplified form of class, suitable for simple data encapsulation without too many complex features.

Combining Structures with Functions

You can initialize a structure through a constructor, and avoid unnecessary copying by passing the structure by reference.

Example

struct

Books

{

    string title

;

    string author

;

    string subject

;

   

int

book_id

;

   

// Constructor

    Books

(

string t, string a, string s,

int

id

)

       

:

title

(

t

)

, author

(

a

)

, subject

(

s

)

, book_id

(

id

)

{

}

   

void

printInfo

(

)

const

{

       

cout

<<

"Book title: "

<<

title

<<

endl

;

       

cout

<<

"Book author: "

<<

author

<<

endl

;

       

cout

<<

"Book category: "

<<

subject

<<

endl

;

       

cout

<<

"Book ID: "

<<

book_id

<<

endl

;

   

}

}

;

void

printBookByRef

(

const

Books

&

book

)

{

    book.

printInfo

(

)

;

}