Home>

C++ structure type definition and initialization

Sometimes it is necessary to combine different types of data into an organic whole,For user's convenience.The data of these combinations in a whole are interconnected.For example, a student's student ID, name, gender, age, grade, home address, etc.Are all attributes of this student,See picture

You can see that the student number (num), name (name), gender (sex), age (age), grade (score), and address (addr) are related to the student whose name is "li fun".If num, name, sex, age, score, addr are defined as independent variables in the program,It is difficult to reflect the internal connection between them.They should be organized into a composite item,A combination item contains several data items of different types (which can of course be the same).C and C++ allow users to specify such a data type,It is called a structure.It is equivalent to records in other high-level languages.

For example, the following declaration can be used to establish the data type shown in the figure.

struct student //declare a struct type student
{
  int num;//include an integer variable num
  char name [20];//Includes a character array name, which can hold 20 characters
  char sex;//include a character variable sex
  int age;//include an integer variable age
  float score;//Include a single precision variable
  char addr [30];//Includes a character array addr, which can hold 30 characters
};//There is a semicolon at the end

In this way, the programmer declares a new structure type student (struct is a keyword that must be used when declaring a structure type,Cannot be omitted), it declares to the compilation system:this is a struct type,It includes different types of data items such as num, name, sex, age, score, addr.It should be stated that student is a type name,It can be used to define variables like the standard types provided by the system (such as int, char, float, double, etc.)It's just that the structure type needs to be declared by the user in advance.

The general form for declaring a structure type is:

 struct structure type name {member list column};

The structure type name is used to mark the structure type.In the above statement, student is the structure type name.Inside the braces are all members of the structure, which make up a specific structure.In the above example, num, name, sex, score, etc. are all members of the structure.When declaring a structure type, you must make a type declaration for each member, that is, the type name member name;each member is also called a field in the structure. Member table columns are also called domain tables.

The declaration of the structure type is usually at the beginning of the file.Before all functions (including the main function) so that all functions in this file can use it to define variables.Of course, you can also declare struct types in functions.

In C, the members of a structure can only be data (as represented in the example above). C++ extends this,Structure members can include both data (ie data members) and functions (ie function members) to accommodate object-oriented programming.

But because C++ provides class types, in general,No need to use structures with functions,Therefore, in this chapter, we only introduce structures with only data members.Structures containing function members will be described in subsequent chapters.

Definition method of structure type variable and its initialization

The above just specifies a structure type,It is equivalent to a model,There is no specific data.The system also does not allocate actual memory units for it to be able to use data of structure type in the program.Variables of structure type should be defined,And store specific data in it.

The method of defining structure type variables can adopt the following three methods to define structure type variables.

1) declare the structure type first and then define the variable name

If a structure type student has been defined above, you can use it to define structure variables.Such as:

In the C language, when defining structure variables,To prefix the structure type name with the keyword sttuct, c++ retains the usage of c, such as:

struct student studentl, student2;

The reader is encouraged to use the new method proposed by C++ when writing C++ programs.That is, it is not necessary to add the keyword struct when defining a structure variable, which is more convenient to use,And it is consistent with the use of class names to define class objects introduced in Chapter 8.

The above defines student1 and student2 as variables of structure type student, that is, they have a structure of type student.as the picture shows.

After defining the structure variables,The system allocates a memory unit for it.For example, student1 and student2 each occupy 63 bytes in memory (4 + 20 + 1 + 4 + 4 + 30=63).

2) Define variables while declaring the type.E.g:

struct student //declares the structure type student
{
  int num;
  char name [20];
  char sex;
  int age;
  float score;
  char addr [30];
} student1, student2;//Define the two student1, student2 variables of the structure type student

The general form of this form of definition is:

struct structure name
{
  Member list
} Variable name table column;

3) Directly define structure type variables.Its general form is:

struct //Note that there is no struct type name
{
  Member list
} Variable name table column;

Although this method is legal,But rarely used.The (1) method of defining a type and then a variable is advocated. The procedure is relatively simple,The structure type is only used in this document,The second method (2) can also be used.

Regarding structure types,There are several points to explain:

1) Don't mistake any structure type for the same structure.In fact, each structure type has its own structure,Many specific structure types can be defined.

2) Types and variables are different concepts,Don't be confused.You can only assign values ​​to members in struct variables,You cannot assign a value to a structure type.At compile time,No space is allocated for types,Allocate space for variables only.

3) The members in the structure (that is, "domain") can be used alone,Its role and status are equivalent to ordinary variables.

4) A member can also be a structure variable.Such as:

struct date //declare a struct type date
{
  int month;
  int day;
  int year;
};
struct student //Declares a struct type student
{
  int num;
  char name [20];
  char sex;
  int age;
  date birthday;
  char addr [30];
} student1, student2;//Define student1 and student2 as variables of structure type student

First declare a date type, which represents "date", including 3 members:rnomh (month), day (day), year (year). Then when declaring the studcm type, specify the member birthday as the date type. The structure of student is shown in the figure.The declared type date, like other types (such as im, char), can also be used to define the type of a member.

5) The member name in the structure can be the same as the variable name in the program.But the two are not related.For example, the program can define another integer variable num, which is different from num in student.Do not affect each other.

Structure variable initialization

Like other types of variables,You can specify initial values ​​for structure variables when you define them.Such as:

struct student
{
  int num;
  char name [20];
  char sex;
  int age;
  float score;
  char addr [30];
} student1={10001, "zhang xin", "m", 19, 90.5, "shanghai"};

In this way, the data in the variable student1 is shown in the figure.

It can also take the form of declaring the type separately from defining the variable,Initialize when defining variables:

student student2={10002, "wang li", "f", 20, 98, "beijing"};//student is the declared structure type

c++ structure variable referenceAfter defining the structure variables,You can of course reference this variable,There are several commonly used methods.

1) You can assign the value of one structure variable to another structure variable with the same structure.

For example, student1 and student2 above are both student type variables.Can be assigned like this:

 student1=student2;

2) You can reference the value of a member of a structure variable.

For example, student1.num represents the value of a member of the structure variable student1,If the value of student1 is as shown in the figure,The value of student1.num is 10001.

The general way to reference members in a structure variable is:

 Structure variable name.
Member name

For example, you can assign values ​​to members of variables:

 student1.num=10010;

3) If the member is also a struct type,You need to use several member operators,Find the lowest level members one by one.

For example, for the structure variable student1 defined above, you can access the members as follows:

 student1.num (refers to the num member in the structure variable student1)

If i want to reference the month member of the birthday member in the student1 variable, you cannot write it as student1.month, you must refer to it step by step,which is

 student1.birthday.month=12;(referring to the month member of the birthday member in the structure variable student1)

4) A structure variable cannot be input and output as a whole.

For example, student1 and student2 have been defined as structure variables,And they already have values.You cannot attempt to output the value of each member in a structure variable like this

 cin>student1;

Input and output can only be made separately for each member of a structure variable.

5) Members of structure variables can perform various operations just like ordinary variables (depending on their type, the types of operations that can be performed are determined). E.g:

 student2.score=student1.score;
  sum=student1.score + student2.score;
  student1.age ++;
  ++ student1.age;

Since the "." Operator has the highest priority,student1.age ++ is equivalent to (student1.age) ++. ++ is an auto-add operation on student1.age,Rather than auto-adding age first.

6) You can reference the address of a member of a structure variable,You can also reference the address of a structure variable.Such as:

 cout<&student1;//output the first address of student1
  cout<&student1.age;//output the address of student1.age

The address of the structure variable is mainly used as a function parameter,Pass the address of the structure variable to the formal parameter.

[Example] Refer to members in structure variables.

#include<iostream>
using namespace std;
struct date //declaration of structure type date
{
  int month;
  int day;
  int year;
};
struct student //declaration structure type student
{
  int num;
  char name [20];
  char sex;
  date birthday;//Declare birthday as a member of type date
  float score;
} student1, student2={10002, "wang li", "f", 5,23,1982,89.5};
//Define student1, student2, and initialize student2
int main ()
{
  student1=student2;//The value of each member of student2 is assigned to the corresponding member of student1
  cout<student1.num<endl;//output the value of the num member in student1
  cout<student1.name<endl;//output the value of the name member in student1
  cout<student1.sex<endl;//output the value of sex member in student1
  cout<student1.birthday.month<<"//<student1.birthday.day<<"<<student1.birthday.year<endl;//Output of student1 values ​​of birthday members
  cout<student1.score<endl;
  return 0;
}

The results are as follows:

10002
wang li
f
5/23/1982
89.5
c
  • Previous C ++ dynamically allocate and deallocate memory and structure types as function parameters
  • Next Solution to cannot enable Enable property of Checkbox control in Aspnet using JavaScript script