A brief analysis of the definition initialization and reference of structs in C++

  • 2020-04-02 01:26:02
  • OfStack

Definition:
A struct is a collection of data of the same type or different types, also known as a structure.

The form that declares a struct type is:


struct Student{      //Declare a struct type Student
 int num;         //Declare an integer variable num
 char name[20];   //Declare a character array name
 char sex;        //Declare a character variable sex
 int age;         //Declare an int variable age
 float score;     //Declare a single - precision variable
 char addr[30];   //Declare a character array addr
}

Definition and initialization of structure type variables

Method to define structural variables:
(1) declare the structure type before defining the variable name


#include<iostream>
using namespace std;
int main(){
 struct Student{      //Declare a struct type Student
  int num;         //Declare an integer variable num
  char name[20];   //Declare a character array name
  char sex;        //Declare a character variable sex
  int age;         //Declare an int variable age
  float score;     //Declare a single - precision variable
     char addr[30];   //Declare a character array addr
   };
   Student student1,student2;//Define the struct type variables student1 and student2
   cout<<sizeof(Student)<<endl;
   cout<<sizeof(student1)<<endl;
   cout<<sizeof(student2)<<endl;     
   return 0;
}


< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/201309020906235.jpg ">

After the struct variables are defined, the system allocates memory units for them. You can use the sizeof function to see the number of bytes allocated, which varies between different compilation systems.

(2) define variables while declaring types


#include<iostream>
using namespace std;
int main(){
 struct Student{      //Declare a struct type Student
  int num;         //Declare an integer variable num
  char name[20];   //Declare a character array name
  char sex;        //Declare a character variable sex
  int age;         //Declare an int variable age
  float score;     //Declare a single - precision variable
     char addr[30];   //Declare a character array addr
   }student1,student2;//Declare the variables student1 and student2
   cout<<sizeof(Student)<<endl;
   cout<<sizeof(student1)<<endl;
   cout<<sizeof(student2)<<endl;     
   return 0;
}

(3) directly define structure type variables

#include<iostream>
using namespace std;
int main(){
 struct {      //Declare a struct type Student
  int num;         //Declare an integer variable num
  char name[20];   //Declare a character array name
  char sex;        //Declare a character variable sex
  int age;         //Declare an int variable age
  float score;     //Declare a single - precision variable
     char addr[30];   //Declare a character array addr
   }student1,student2;//Declare the variables student1 and student2
   cout<<sizeof(student1)<<endl;
   cout<<sizeof(student2)<<endl;     
   return 0;
}


< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/201309020906236.jpg ">

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

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

(2) the members in the structure variables (i.e., "domain") can be used separately, whose function and status are equivalent to ordinary variables of the same type.

(3) the member of a structure can also be a structure variable.


#include<iostream>
using namespace std;
 struct Date{        //Declare a struct type Date
  int month;      //Month in date
  int day;        //Day in the day
  int year;       //Year in date
 }; 
 struct Student{      //Declare a struct type Student
  int num;         //Declare an integer variable num
  char name[20];   //Declare a character array name
  char sex;        //Declare a character variable sex
  int age;         //Declare an int variable age
  Date birthday;   //Date is a struct type, and birthday is a variable of the type Date
  float score;     //Declare a single - precision variable
     char addr[30];   //Declare a character array addr
   };
int main(){
   Student qianshou;
   Date     riqi;
   cout<<sizeof(riqi)<<endl; 
   cout<<sizeof(qianshou)<<endl;  
   return 0;
}


< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/201309020906238.jpg ">

(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, the program can define another plastic variable, which is independent of the num in student.

Initialization of structure variables
(1) when defining the structure, specify the initial value of the structure variable


 struct Student{      //Declare a struct type Student
  int num;         //Declare an integer variable num
  char name[20];   //Declare a character array name
  char sex;        //Declare a character variable sex
  int age;         //Declare an int variable age
  float score;     //Declare a single - precision variable
     char addr[30];   //Declare a character array addr
       } student1={
             10001,
            "qianshou",
             'm',
             19,
             "100",
             "JiNan"
       };

(2) initialization when defining variables (this method is more commonly used)

 struct Student{      //Declare a struct type Student
  int num;         //Declare an integer variable num
  char name[20];   //Declare a character array name
  char sex;        //Declare a character variable sex
  int age;         //Declare an int variable age
  float score;     //Declare a single - precision variable
     char addr[30];   //Declare a character 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>   };

A reference to a struct variable
After you have defined a struct variable, you can refer to it.

(1) refer to the value of a member in the structure variable

Method of reference: structure variable name. Member name

Where ". "is the member operator, which has the highest priority of all operators.


#include<iostream>
using namespace std;
 struct Date{        //Declare a struct type Date
  int month;      //Month in date
  int day;        //Day in the day
  int year;       //Year in date
 }; 
 struct Student{      //Declare a struct type Student
  int num;         //Declare an integer variable num
  char name[20];   //Declare a character array name
  char sex;        //Declare a character variable sex
  int age;         //Declare an int variable age
  Date birthday;   //Date is a struct type, and birthday is a variable of the type Date
  float score;     //Declare a single - precision variable
     char addr[30];   //Declare a character 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;
}


< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/201309020906249.jpg ">

If a member club is also a struct type, the lowest member is found level by level with several member operators.

Such as:


  cout<<one.birthday.month<<"/"<<one.birthday.day<<"/"<<one.birthday.year<<endl;

(2) the value of one structure variable can be paid to another structure variable with similar mechanism.

#include<iostream>
using namespace std;
 struct Date{        //Declare a struct type Date
  int month;      //Month in date
  int day;        //Day in the day
  int year;       //Year in date
 }; 
 struct Student{      //Declare a struct type Student
  int num;         //Declare an integer variable num
  char name[20];   //Declare a character array name
  char sex;        //Declare a character variable sex
  int age;         //Declare an int variable age
  Date birthday;   //Date is a struct type, and birthday is a variable of the type Date
  float score;     //Declare a single - precision variable
     char addr[30];   //Declare a character 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 the address of a struct variable member.

#include<iostream>
using namespace std;
 struct Date{        //Declare a struct type Date
  int month;      //Month in date
  int day;        //Day in the day
  int year;       //Year in date
 }; 
 struct Student{      //Declare a struct type Student
  int num;         //Declare an integer variable num
  char name[20];   //Declare a character array name
  char sex;        //Declare a character variable sex
  int age;         //Declare an int variable age
  Date birthday;   //Date is a struct type, and birthday is a variable of the type Date
  float score;     //Declare a single - precision variable
     char addr[30];   //Declare a character 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;
}


< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/2013090209062410.jpg ">

A small example:

#include<iostream>
using namespace std;
 struct Date{        //Declare a struct type Date
  int month;      //Month in date
  int day;        //Day in the day
  int year;       //Year in date
 }; 
 struct Student{      //Declare a struct type Student
  int num;         //Declare an integer variable num
  char name[20];   //Declare a character array name
  char sex[5];        //Declare a character variable sex
  int age;         //Declare an int variable age
  Date birthday;   //Date is a struct type, and birthday is a variable of the type Date
  float score;     //Declare a single - precision variable
     char addr[30];   //Declare a character array addr
   };
int main(){
   Student one;
   //The input information
   cout<<" Please enter your student number :";
   cin>>one.num;
   cout<<" Please enter name :";
   cin>>one.name;
   cout<<" Please enter gender :";
   cin>>one.sex;
   cout<<" Please enter age :";
   cin>>one.age; 
   cout<<" Please enter the year of birth   month   Day: ";
   cin>>one.birthday.year;
   cin>>one.birthday.month;
   cin>>one.birthday.day; 
   cout<<" Please enter your grade: ";
   cin>>one.score;
   cout<<" Please enter address: ";
   cin>>one.addr; 
   //Output information
   cout<<"n Here is your information n"; 
   cout<<" Student id :"<<one.num<<endl;
   cout<<" The name :"<<one.name<<endl;
   cout<<" gender :"<<one.sex<<endl;
   cout<<" age :"<<one.age<<endl;
   cout<<" birthday :"<<one.birthday.year<<"/"<<one.birthday.month<<"/"<<one.birthday.day<<endl;
   cout<<" results :"<<one.score<<endl;
   cout<<" address :"<<one.addr<<endl;  
   return 0;
}


< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/2013090209062411.jpg ">


Related articles: