Analysis of the definition, initialization and reference _c language of the structure in C + +

Source: Internet
Author: User

Defined:
A structure body (struct) is a collection of data consisting of a series of data of the same type or different type, also called a structure.

The form of declaring a struct type is:

Copy Code code as follows:

struct student{//declaration of a struct type Student
int num; Declare an integer variable num
Char name[20]; Declares an array of characters name
char sex; Declare a character type variable sex
int age; Declare an integer variable age
Float score; Declaring a single-precision variable
Char addr[30]; Declares a character-type array addr
}

Definition method and initialization of structural body type variable

To define a structural body variable:
(1) First declare the struct type in the definition variable name

Copy Code code as follows:

#include <iostream>
using namespace std;
Int main () {
 struct student{     //declares a struct type Student
  int num;& nbsp;       //Declare an integer variable num
  char name[20];  // Declares a character array name
  char sex;       //declaration of a character variable sex
   int age;        //Declaring an integer variable age
  float score;     //Declare a single variable
     char addr[30];  //declaration of a character array addr
  };
   Student student1,student2;//defines structural body type variables student1 and Student2
   cout<<sizeof ( Student) <<endl;
   cout<<sizeof (student1) <<endl;
   cout<<sizeof (student2) <<endl;    
   return 0;
}


After the structure variable is defined, the system allocates the internal deposit. (You can use the sizeof function to view the number of bytes allocated, different compilation systems differ)

(2) Defining variables at the same time as declaring types

Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
struct student{//declaration of a struct type Student
int num; Declare an integer variable num
Char name[20]; Declares an array of characters name
char sex; Declare a character type variable sex
int age; Declare an integer variable age
Float score; Declaring a single-precision variable
Char addr[30]; Declares a character-type array addr
}student1,student2;//declaring variables Student1 and Student2
Cout<<sizeof (Student) <<endl;
Cout<<sizeof (student1) <<endl;
Cout<<sizeof (Student2) <<endl;
return 0;
}

(3) Direct definition of structural body type variables
Copy Code code as follows:

#include <iostream>
using namespace Std;
int main () {
struct {//declares a struct type student
int num; Declare an integer variable num
Char name[20]; Declares an array of characters name
char sex; Declare a character type variable sex
int age; Declare an integer variable age
Float score; Declaring a single-precision variable
Char addr[30]; Declares a character-type array addr
}student1,student2;//declaring variables Student1 and Student2
Cout<<sizeof (student1) <<endl;
Cout<<sizeof (Student2) <<endl;
return 0;
}


This definition method is legal, but not commonly used. The first method is more commonly used.

A few things to note about the types of structures:
(1) The type and the variable are different concepts, do not confuse. You can assign values only to members in a struct variable, not to the struct body type.

(2) The member (that is, "field") of a struct variable can be used alone, and its function and status are equivalent to the ordinary variable of the same type.

(3) A member of a struct can also be a structural variable.

Copy Code code as follows:

#include <iostream>
using namespace Std;
struct date{//declaration of a struct type Date
int month; The month in the date
int day; Days in the date
int year; Year in the date
};
struct student{//declaration of a struct type Student
int num; Declare an integer variable num
Char name[20]; Declares an array of characters name
char sex; Declare a character type variable sex
int age; Declare an integer variable age
Date birthday; Date is a struct type, birthday is a variable of the type of date
Float score; Declaring a single-precision variable
Char addr[30]; Declares a character-type array addr
};
int main () {
Student Qianshou;
Date Riqi;
Cout<<sizeof (Riqi) <<endl;
Cout<<sizeof (Qianshou) <<endl;
return 0;
}




(5) The member names in the struct body can be the same as the variable names in the program, but they are not related.

For example, you can define a different shape variable in a program, and he is not the same as the NUM in student.

2 Initialization of structural variables
(1) Specifying the initial value of a struct variable when defining a struct body

Copy Code code as follows:

struct student{//declaration of a struct type Student
int num; Declare an integer variable num
Char name[20]; Declares an array of characters name
char sex; Declare a character type variable sex
int age; Declare an integer variable age
Float score; Declaring a single-precision variable
Char addr[30]; Declares a character-type array addr
} student1={
10001,
"Qianshou",
' m ',
19,
"100",
"Jinan"
};

(2) Initialize when defining variables (this method is more common)
Copy Code code as follows:

struct student{//declaration of a struct type Student
int num; Declare an integer variable num
Char name[20]; Declares an array of characters name
char sex; Declare a character type variable sex
int age; Declare an integer variable age
Float score; Declaring a single-precision variable
Char addr[30]; Declares a character-type array addr
};
Student student1={
<span style= "White-space:pre" > </SPAN> 10001,
<span style= "White-space:pre" > </SPAN> "Qianshou",
<span style= "White-space:pre" > </SPAN> ' m ',
<span style= "White-space:pre" > </SPAN> 19,
<span style= "White-space:pre" > </SPAN> "100",
<span style= "White-space:pre" > </SPAN> "Jinan"
<span style= "White-space:pre" > </SPAN>};

Reference to a struct variable
After you have defined a struct variable, you can reference that variable.

(1) A value that refers to a member of a struct-body variable

Reference mode: struct variable name. Member Name

where "." is the member operator, which has the highest precedence in all operators.

Copy Code code as follows:

#include <iostream>
using namespace Std;
struct date{//declaration of a struct type Date
int month; The month in the date
int day; Days in the date
int year; Year in the date
};
struct student{//declaration of a struct type Student
int num; Declare an integer variable num
Char name[20]; Declares an array of characters name
char sex; Declare a character type variable sex
int age; Declare an integer variable age
Date birthday; Date is a struct type, birthday is a variable of the type of date
Float score; Declaring a single-precision variable
Char addr[30]; Declares a character-type array addr
};
int main () {
Student one={001, "Qianshou", ' m ', 19,10,1,1993,100, "Jinan"};
cout<<one.num<<endl;
cout<<one.name<<endl;
cout<<one.sex<<endl;
cout<<one.age<<endl;
cout<<one.birthday.month<< "/" <<one.birthday.day<< "/" <<one.birthday.year<< Endl
cout<<one.score<<endl;
cout<<one.addr<<endl;
return 0;
}




If a member of the society is also a struct type, it is possible to find the lowest level of members at the first level with several member operators.

For example:

Copy Code code as follows:

cout<<one.birthday.month<< "/" <<one.birthday.day<< "/" <<one.birthday.year<< Endl

(2) The value of a struct variable can be paid to another structural body variable with an identical mechanism.
Copy Code code as follows:

#include <iostream>
using namespace Std;
struct date{//declaration of a struct type Date
int month; The month in the date
int day; Days in the date
int year; Year in the date
};
struct student{//declaration of a struct type Student
int num; Declare an integer variable num
Char name[20]; Declares an array of characters name
char sex; Declare a character type variable sex
int age; Declare an integer variable age
Date birthday; Date is a struct type, birthday is a variable of the type of date
Float score; Declaring a single-precision variable
Char addr[30]; Declares a character-type array addr
};
int main () {
Student two={1, "Qianshou", ' m ', 19,10,01,1993,100, "Jinan"};
Student One=two;
cout<<one.num<<endl;
cout<<one.name<<endl;
cout<<one.sex<<endl;
cout<<one.age<<endl;
cout<<one.birthday.month<< "/" <<one.birthday.day<< "/" <<one.birthday.year<< Endl
cout<<one.score<<endl;
cout<<one.addr<<endl;
return 0;
}

(3) You can refer to the address of a struct variable, or you can refer to the address of a struct variable member.
Copy Code code as follows:

#include <iostream>
using namespace Std;
struct date{//declaration of a struct type Date
int month; The month in the date
int day; Days in the date
int year; Year in the date
};
struct student{//declaration of a struct type Student
int num; Declare an integer variable num
Char name[20]; Declares an array of characters name
char sex; Declare a character type variable sex
int age; Declare an integer variable age
Date birthday; Date is a struct type, birthday is a variable of the type of date
Float score; Declaring a single-precision variable
Char addr[30]; Declares a character-type array addr
};
int main () {
Student two={1, "Qianshou", ' m ', 19,10,01,1993,100, "Jinan"};
Student &one=two;
one.num++;
one.birthday.day+=10;
cout<<two.num<<endl;
cout<<two.name<<endl;
cout<<two.sex<<endl;
cout<<two.age<<endl;
cout<<two.birthday.month<< "/" <<two.birthday.day<< "/" <<two.birthday.year<< Endl
cout<<two.score<<endl;
cout<<two.addr<<endl;
return 0;
}




A small example:
Copy Code code as follows:

#include <iostream>
using namespace Std;
struct date{//declaration of a struct type Date
int month; The month in the date
int day; Days in the date
int year; Year in the date
};
struct student{//declaration of a struct type Student
int num; Declare an integer variable num
Char name[20]; Declares an array of characters name
Char sex[5]; Declare a character type variable sex
int age; Declare an integer variable age
Date birthday; Date is a struct type, birthday is a variable of the type of date
Float score; Declaring a single-precision variable
Char addr[30]; Declares a character-type array addr
};
int main () {
Student one;
Enter information
cout<< "Please enter the school number:";
cin>>one.num;
cout<< "Please enter your name:";
cin>>one.name;
cout<< "Please enter Gender:";
cin>>one.sex;
cout<< "Please enter the Age:";
cin>>one.age;
cout<< "Please enter the date of the birthday:";
cin>>one.birthday.year;
cin>>one.birthday.month;
cin>>one.birthday.day;
cout<< "Please input your score:";
cin>>one.score;
cout<< "Please enter the address:";
cin>>one.addr;
Output information
cout<< "\ n below is your message \ n";
cout<< "School No.:" <<one.num<<endl;
cout<< "Name:" <<one.name<<endl;
cout<< "Sex:" <<one.sex<<endl;
cout<< "Age:" <<one.age<<endl;
cout<< "Birthday:" <<one.birthday.year<< "/" <<one.birthday.month<< "/" << one.birthday.day<<endl;
cout<< "Score:" <<one.score<<endl;
cout<< "Address:" <<one.addr<<endl;
return 0;
}


Related Article

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.