Foundation and use of structure

Keywords: C C# Database

Declaration of structure type

Basic knowledge of structure

An array is a collection of elements of the same type

A structure is a collection of values that contain arrays, and these values are called member variables. Each member of the structure can be a different type of variable. Members can be scalars, arrays, pointers, or even other structures.

Declaration of structure

There must be a member inside the structure

struct tag
{
    member_list;//Member list
}variable_list;//Variable list

for example
Describe a student's structure:

struct Stu
{
 char name[20];//name
 int age;//Age
 char sex[5];//Gender
 char id[20];//Student number
};//Semicolons cannot be lost

This defines the type of a student structure

Definition and initialization of structure variables

definition

  • Structure variables can be defined in other places or when types are declared, but the variables defined in the global scope are global variables and are not recommended.
    For example:
struct Point
{
 int x;
 int y; 
 } p1; //Define the variable p1 while declaring the type
struct Point p2; //Define structure variable p2
  • You can rename the structure type with typedef to facilitate the definition of structure variables and pointers in the future.
    For example:
typedef struct Stu
{
 char name[20];//name
 int age;//Age
 char sex[5];//Gender
 char id[20];//Student number
}Stu;//This is not a variable, but a renamed structure type

At this time, you can use struct Stu or Stu to define variables, but Stu is more concise and convenient

  • You can also rename multiple type names at the same time, and all of them can be used
    For example:
typedef struct Stu
{
    char name[20];//name
    int age;//Age
    char sex[5];//Gender
    char id[20];//Student number
}Stu, * a, * del, cur;

int main()
{
    Stu z;
    a x;
    del c;
    cur v;
    struct Stu b;
    return 0;
}

analysis

At this time, the type of z v b is Stu, which is a structural variable, while the type of x c is Stu *, which is a pointer to a structural variable.

initialization

//Initialization: define variables and assign initial values at the same time.
struct Point p3 = {x, y};
struct Stu        //Type declaration
{
char name[15];//name
 int age;      //Age
};
struct Stu s = {"zhangsan", 20};//Initialization, each member is separated by a, sign
struct Node
{
 int data;
 struct Point p;
 struct Node* next; //You can define a structure pointer inside a structure, but you cannot define the structure itself
}n1 = {10, {4,5}, NULL}; //Structure nesting initialization
struct Node n2 = {20, {5, 6}, NULL};//Structure nesting initialization

Access to structure members

  • Structural variables access members. Members of structural variables are accessed through the point operator (.). The point operator accepts two operands. For example:
typedef struct Stu
{
    char name[20];
    int age;
}Stu;

int main()
{
    Stu s;
    strcpy(s.name, "zhangsan");//Use. To access the name member
    s.age = 20;//Use. To access age members

    return 0;
  • Structure pointer accesses members pointing to variables. Sometimes we get a pointer to a structure instead of a structure variable.
    How to access members. As follows:
typedef struct Stu
{
    char name[20];
    int age;
}Stu;
void print(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()
{
    Stu s = {"zhangsan", 20};
    print(&s);//Structure address transfer parameter
    return 0; }
  • Pointers to nested structures access internal structure members
struct Tea
{
    char num[20];
    int age;
};

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

int main()
{
    Stu s;
    strcpy(s.name, "zhangsan");//Use. To access the name member
    Stu* ps = &s;
    ps->teacher.age = 20;//Use Stu's pointer to access the teacher structure member inside, and use. To access the age member

    return 0;
}

Structural transmission parameters

Upper code

struct S {
 int data[1000];
 int num;
};
struct S s = {{1,2,3,4}, 1000};
//Structural transmission parameters
void print1(struct S s) {
 printf("%d\n", s.num);
}
//Structure address transfer parameter
void print2(struct S* ps) {
 printf("%d\n", ps->num);
}
int main()
{
 print1(s);  //Transmission structure
 print2(&s); //Transmission address
 return 0; }

Which of the print1 and print2 functions above is better?
The answer is: the print2 function is preferred. reason:

When a function passes parameters, the parameters need to be pressed on the stack. If the structure is too large when passing a structure object, the system overhead of parameter stack pressing is relatively large, which will lead to performance degradation. The array parameter is actively reduced to a pointer, but if the structure does not pass a pointer, it will not be reduced to a pointer. Attention should be paid to parameter transmission when using.

Conclusion: when the structure passes parameters, the address of the structure should be passed.

Posted by stageguys on Mon, 13 Sep 2021 11:24:25 -0700