Type definition and initialization of the struct in C + + and variable reference _c language

Source: Internet
Author: User

Definition and initialization of C + + struct type

It is sometimes necessary to combine different types of data into an organic whole for easy user use. The data of these combinations in a whole are interrelated. For example, a student's number, name, gender, age, grade, home address and other items, are the attributes of this student, see figure

You can see student number (num), name (name), Gender (sex), Age, Grade (score), address (addr) are related to students whose name is "Li Fun". If you define NUM,NAME,SEX,AGE,SCORE,ADDR as separate variables in your program, it is difficult to reflect the intrinsic relationship between them. They should be organized into a combination item that contains several data items of different types (and of course can be the same) in a composite item. C and C + + allow the user to specify such a data type themselves, which is called the struct body. It is the equivalent of a record in other advanced languages.

For example, you can use the following declaration to establish a data type as shown in the illustration.

struct student//declares a struct type Student
{
  int num;//includes an integer variable num
  char name[20];//includes a character array name, which can hold 20 characters
  char sex; Includes a character variable sex
  int age;  Includes an integer variable age
  float score;  Includes a single variable
  char addr[30];//includes a character array addr, which can hold 30 characters
};//finally a semicolon


In this way, the programmer declares a new struct type student (struct is the keyword that must be used when declaring the struct type), and it declares to the compiling system: This is a struct type, which includes num, name, sex, age, score, Different types of data items, such as addr. It should be noted that student is a type name that can be used to define variables, as well as the standard types provided by the system (such as int, char, float, double), except that the struct type needs to be declared by the user themselves beforehand.

The general form of declaring a struct type is:

  struct structure body type name {member table column};

The name of the struct type is used as a marker for the type of struct body. The student in the above declaration is the struct type name. Inside the braces are all members (member) of the struct body, which form a specific structure. The Num,name,sex,score in the example above are all members of the structure body. Declaring a struct type must have a type declaration for each member, a type name member name, and each member is also known as a field in the structure body. A member table column is also called a domain table.

The position of the declaring struct type is generally 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 a struct type in a function.

In C, the members of a struct can only be data (as indicated in the example above). C + + expands this, the member of the structure can include data (namely data member), can include function (namely function member), adapt to object-oriented programming.

However, since C + + provides classes (class) types and, in general, you do not need to use a structure with functions, you will only introduce a struct with a data member in this chapter, and the structure that contains the function members is described in a later section.
Definition method of struct type variable and its initialization

The above only specifies a type of struct, which is equivalent to a model, but there is no specific data, the system does not allocate the actual memory unit in order to be able to use the structure type of data in the program, you should define the variables of the structure type and store the specific data.

The method that defines the structure body type variable can take the following 3 ways to define a variable of the struct type.

1) First declare the structure type and then define the variable name
If a struct type student is defined above, it can be used to define the structural body variables. Such as:

In C, when you define a struct variable, you keep the use of C in front of the struct type name with the keyword sttuct,c++, such as:
struct Student studentl, Student2;

To encourage readers to write C + + programs, use the new method proposed by C + +, that is, it is more convenient to use the keyword struct when defining the structure variables, and it is consistent with the use of class objects as defined in chapter 8th.

The above defines the variables that student1 and Student2 are student for struct type, that is, they have student type of structure. As shown in the figure.

After the structure variables are defined, the system allocates the internal deposit. For example, Student1 and Student2 each account for 63 bytes (4+20+1+4+4+30=63) in memory.

2 define variables at the same time as declaring types. For example:

struct Student//Declaration struct type Student
{
  int num;
  Char name[20];
  char sex;
  int age;
  float score;
  Char addr[30];
} Student1, Student2; A variable student1,student2 defining the student of two structural body types

The general form of this form of definition is:

struct structure Body name
{
  member table column
} variable name table column;


3 directly define the structural body type variables. Its general form is:

struct//Note there is no structural body type name
{
  member table column
} variable name table column;

Although this method is legal, it is rarely used. The first (1) method of defining a variable after defining a type is advocated. In cases where the program is simpler and the struct type is only used in this document, the method can also be used in the first (2).

For structural types, there are a few points to note:
1 do not mistakenly assume that all structural types have the same structure. In fact, each type of struct has its own structure, which can be defined in many specific types of struct bodies.

2 types and variables are different concepts, do not confuse. You can assign values only to members in a struct variable, not to the struct body type. At compile time, there is no space allocated for the type, and only space is allocated for the variable.

3 to the members of the structure (that is, "domain"), can be used alone, its role and status is equivalent to ordinary variables.

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

struct Date//declares 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 variables that Student1 and Student2 are student for struct type

First declare a date type, which represents the date, including 3 Members: RNOMH (month), Day (days), year (years). The member birthday is then specified as the date type when declaring the STUDCM type. The structure of the student is shown in the figure. The declared type date and other types, such as Im,char, can also be used to define the type of the member.

5 The member names in the structure can be the same as the variable names in the program, but they are not related. For example, a program can define an integer variable num, which is different from the NUM in student.

Initialization of structural variables

As with other type variables, you can specify an initial value for a struct variable at definition. Such as:

struct Student
{
  int num;
  Char name[20];
  char sex;
  int age;
  float score;
  Char addr[30];
} student1={10001, "Zhang xin", ' M ', 90.5, "Shanghai"};

This way, the data in the variable student1 is shown in the figure.

You can also take the form of declaring a type that is separate from the defined variable, and initialize the variable when it is defined:

Student Student2 = {10002, "Wang Li", "F", "N", "Beijing"}; Student is a declared type of struct body

Reference to C + + struct-body variables
after defining the structure variables, of course, you can refer to this variable, the common methods are the following several.

1 The value of a struct variable can be assigned to another struct variable with the same structure.

As the above student1 and Student2 are all variables of the student type, you can assign values like this:

  Student1= Student2;

2 can refer to the value of a member of a struct variable.

For example, Student1.num represents the value of a member in a struct variable student1, and if the Student1 value is shown in the figure, the Student1.num value is 10001.

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

  struct variable name. Member Name


For example, you can assign values to a member of a variable:

  student1.num=10010;

3 If the member itself is also a struct type, use several member operators to find the lowest level of members at the first level.

For example, student1 the struct variables defined above, you can access each member in this way:

  Student1.num (refers to the NUM member in the struct variable student1)


If you want to refer to the month member of the birthday member in the STUDENT1 variable, you cannot write student1.month, you must refer to the

  student1.birthday.month=12; (Refer to the month member in the birthday member in the struct variable student1)

4 cannot input and output a struct variable as a whole.

For example, Student1 and student2 are defined as struct variables, and they already have values. Cannot attempt to output the values of each member in a struct variable

  cin>>student1;


Input and output can only be made to individual members in a struct variable.

5 The members of a struct variable can perform various operations (depending on the type of operation that can be performed) as a normal variable. For example:

  Student2.score=student1.score;
  Sum=student1.score+student2.score;
  student1.age++;
  ++student1.age;


Because "." Operators have the highest precedence, student1.age++ equivalent to (student1.age) + +. + + is the student1.age, rather than the age of the first self-add operations.

6 You can refer to the address of a struct variable member, or you can refer to the address of a struct variable. Such as:

  cout<<&student1; Output student1
  address cout<<&student1.age;//Output Student1.age


The address of a struct variable is mainly used as a function parameter, and the address of the structural body variable is passed to the formal parameter.

"Example" refers to members in a struct-body variable.

#include <iostream>
using namespace std;
struct date//declares the struct body type Date
{
  int month;
  int day;
  int year;
};
struct student//declares the struct body type Student
{
  int num;
  Char name[20];
  char sex;
  Date birthday; Declare birthday as a member of the date type
  float score;
student1,student2={10002, "Wang Li", ' F ', 5,23,1982,89.5};
Defines the variable Student1,student2 for the student type and initializes
int main ()
{student1=student2 to Student2
  ; Assigns the value of each member of the Student2 to the
  value of the NUM member cout<<student1.num<<endl;//output Student1 of Student1
  cout< <student1.name<<endl; The value of the name member in the output student1
  cout<<student1.sex<<endl;//output values of sex members in the Student1
  cout<< student1.birthday.month<< '/' <<student1.birthday.day<< '/' <<student1.birthday.year< <endl; The value of the birthday members in the output student1
  cout<<student1.score<<endl;
  return 0;
}

The results of the operation are as follows:

10002
Wang Li
f
5/23/1982
89.5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.