当前位置:网站首页>Initial structure

Initial structure

2022-06-23 01:43:00 'Dream_

If you will It's better to start than to give up . Roman.

May we all have our own goals and are making unremitting efforts for them .

-----------------------------------------------------------------------

One 、 Declaration of a structure

One ) Basic knowledge of structure

A structure is a collection of values , These values are called member variables . Each member of a structure can be a variable of a different type .

Two ) Declaration of a structure

Form like :

struct tag   // struct( keyword )+ Structural tag

{

  member-list;  // Member variables

}variable-list;  // Variable list

example :

// Structure

struct Stu
{
    char name[20]; // name
    int age; // Age
    char sex[8]; // Gender
    float score; // achievement
}s1,s2,s3;
// s1,s2,s3 It's through struct Stu Variable created by type
// s1,s2,s3 Global variable

// typedef Type redefinition / Type renaming

typedef struct Stu Stu;
// You can use Stu Instead of struct Stu

int main()
{
    // struct Stu Is the structure type , You can't omit struct
    
    struct Stu s4;
    struct Stu s5;
    // s4,s5 It's a local variable
    
    Stu s6;
    // typedef Can be used after Stu

    return 0;
}


// typedef Another way

struct Stu
{
    char name[20]; // name
    int age; // Age
    char sex[8]; // Gender
    float score; // achievement
}Stu;  // At this time, you can only write the name of the heavy life here , Cannot write variable list

 

3、 ... and ) Type of structure member

  Structure members can be Scalar 、 Array 、 The pointer , Even structures .

Four ) Structure variable definition and initialization

 1.  Structure initialization Curly braces { }

 2. Pointer to the variable Address or Vent pointer NULL

------------------------------------------------------------------------------

Two 、 Access to structure members

1. Structure member access operator :

  •   Structural variable . Structure member name
  • Structure pointer -> Structure member name

 2.  example :

//  Structure access 

struct Stu
{
	char name[20];
	int age;
};

void print(struct Stu* ps) {
	printf("name = %s   age = %d\n", (*ps).name, (*ps).age);
	// Use structure pointers to access members that point to objects 
	printf("name = %s   age = %d\n", ps->name, ps->age);
}

int main()
{
	struct Stu s = { "zhangsan", 20 };
	print(&s);// Structure address transfer parameter 
	return 0;
}

//  If the structure has multiple layers   According to   The pointer -> or  Variable .  Just visit 

 3. modify Member contents of structure variables

  • Members are ordinary : =( assignment )
  •   Members are arrays : strcpy(  ,);  // The header file string.h

------------------------------------------------------------------------------

3、 ... and 、 Structural parameters

One ) Pass on Structural variable

  Pass on Structural variable , namely Value passed : An argument is a temporary copy of a formal parameter .

  • It will waste a lot of space
  • When changing the contents of formal parameters through functions , Arguments don't change , High security

Two ) Pass on Structure pointer

  Pass on Structure pointer , namely    Address transmission .

  • Account for only a 4/8 byte , Less space
  • To improve security , When transferring parameters Add... Before the formal parameter const modification

3、 ... and ) Summary

  When a function passes parameters , Parameters need to be stacked .

  If you pass a structure object , The structure is too large , The system overhead of parameter stack pressing is large , So it will lead to performance degradation

  • Each function call will open up a space in the stack area of memory
  • Memory ( From top to bottom ):

The stack area : local variable 、 Function parameter

Heap area : Dynamic memory development     Such as :malloc 、free 、calloc 、realloc

Static zone : Static variables 、 Global variables

  • Summary : Structural parameters , To transmit Structure address

------------------------ All a person's anger comes from the pain of his incompetence .------------------------

原网站

版权声明
本文为['Dream_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220514206480.html