Structure - Summary

  First, the structure of the following advantages,

 (1) may contain a different type of data structure

(2) the same structure variable is assigned to each other

(3) to save memory space

  Note: When the structure declaration itself does not take up any memory space, only if you use a structure type definition structure variable that you define when computer will allocate memory.

      Direct memory operations to avoid the need for space to open up the stack structure variable space, saving memory.

(4) high efficiency

  According to disassemble the idea of ​​structure in the stack is discrete, and the array is continuous in the stack, and access up structure is relatively faster, but also saw some of the materials that are efficient structure to take space for time, I think, should be stored and accessed in different ways and make it more efficient. (Better to say I hope readers and friends a lot of guidance)

Example: struct data

{

     int num;

     char name;

     float  score;  

};

data  t1,t2;

 

 

A simple piece of code

    
#include " iostream "
#include
" string "
using namespace std;
struct data
{
int num;
char name;
float score;

};
int main()
{
data t1,t2,t3;
// 定义结构体变量
cin >> t1.num >> t1.name >> t1.score;
t2
= t1; // 结构体变量之间相互赋值
t3 = t1;
data
* p; // 结构体指针
p =& t1; // 指针指向结构体变量t1的内存地址
cout << t1.num << endl << t2.name << endl << t3.score << endl; // 把t1所对应的地址
cin >> p -> name >> p -> num >> p -> score;
cout
<< p -> name << endl << p -> num << endl << p -> score << endl;
cout
<< ( * p).name << endl << ( * p).num << endl << ( * p).score << endl;
cin.
get ();

}
10 ;

Second, the use direction of the structure

    Mainly used to contain different types of variables, such as student information registration form, student's name (char), student number (int), Age (int), gender (char), score (float) and so on, this time we need use the structure. And inside the array of data types and lengths must be the same.

 Beginner, there are many places feeling very contradictory, which in the future will gradually in-depth understanding of learning, I hope the reader friends can figure it out, progress together, grow together!

Reproduced in: https: //my.oschina.net/garyun/blog/602975

Guess you like

Origin blog.csdn.net/weixin_34205076/article/details/91774641