Detailed explanation - structure (initial stage of C language)

Keywords: C++ C#

preface

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

The types given by C language (character type, integer type and floating point type) can not express all types in life. When we want to describe a complex thing, the complex thing may not only have one variable, and the types of variables may also be different.

How can we express this complex thing?

This requires us to define a type for this thing - custom type (structure)
Defining the type (structure) - is to digitize complex things - for example, books are specific things - we can use several basic attributes of books (such as book name, author, price and Book Number) to represent this specific thing of books.

The following explains the structure from three aspects: structure declaration, structure access and structure parameter transmission

1, Structure declaration

1. Statement of structure

struct tag(Structure name)
{
 member-list(Structure members);
}variable-list(Structure (variable);

For example, describe a Book:

struct Book  //Book is the name of the structure; struct Book is a struct type
{
	char name[20];      //The middle is a member of the structure
	char auther[20];
	float price;
	char id[20];
}b1;    //b1 is the structure variable

2. Structure member type

The members of a structure can be scalars (integer, floating point), arrays, pointers, or even other structures.

3. Definition and initialization of structure variables

3.1 the first method

This method is to define structure variables and initialization directly behind the structure

#include<stdio.h>

struct Book
{
	char name[20];
	char auther[20];
	float price;
	char id[20];
}b1 = { "cyuyan", "zhangsan", 40.0f, "1001010011" };  //Definition and initialization of structure variables
//b1 is the structure variable
//b1 is a global variable

int main()
{
	;   //b1 (structure variable) can be used here
	return 0;
}

3.2 the second method

This method is to define structure variables and initialization when using

#include<stdio.h>

struct Book   //Declaration of structure variables
{
	char name[20];
	char auther[20];
	float price;
	char id[20];
};

int main()
{
	struct Book b1 = { "cyuyan", "zhangsan", 40.0f, "1001010011" };  //Definition and initialization of structure variables
	struct Book b2 = { "shujujiegou", "lisi", 50.0f, "1110001101" };
	//struct Book -- is a structure type
	//b1,b2 -- structural variables
	//b1,b2 -- local variables
	return 0;
}

We generally use the second method - the definition and initialization of structure variables.

Because:

The second method is to create variables and initialize them when they are used -- avoiding the situation of creating variables but not using them

The second way is to create local variables. The first way is to create global variables - we try to destroy variables when we don't use them.

3.3 using typedef to simplify the name of structure type

#include<stdio.h>

typedef struct Book   //Use the typedef keyword to redefine the type (struct book) to a name (Book)
                      //Book has the same functions as this struct book
{
	char name[20];
	char auther[20];
	float price;
	char id[20];
}Book;

int main()
{
	Book b1 = { "cyuyan", "zhangsan", 40.0f, "1001010011" };   //initialization
	struct Book b2 = { "shujujiegou", "lisi", 50.0f, "1110001101" };
	return 0;
}

The typedef keyword can change not only the name of the structure, but also any other type of variable

3.4 structure nesting

#include<stdio.h>

struct Book   //Basic properties of structure description
{
	char name[20];
	char auther[20];
	float price;
	char id[20];
};

struct Point   //Represents a coordinate point
{
	int x;
	int y;
	struct Book;  //A structure is nested within a structure
};

int main()
{
	struct Point p1 = { 10, 20, { "cyuyan", "zhangsan", 40.0f, "1001010011"} }; //Initialization of structure nesting
	return 0;
}

2, Access to structure

We talked about how to create and initialize a structure, and how to access its members after the structure is created?
Here's how to access the structure.

Structure variable access member
Members of structure variables are accessed through the point operator (.). The point operator accepts two operands.

Structure pointers access members that point to variables
Structure pointers are accessed through the point operator (- >). The point operator accepts two operands.

give an example:

#include<stdio.h>

struct Book
{
	char name[20];
	char auther[30];
	float price;
	char id[20];
};

struct Point
{
	int x;
	int y;
	struct Book b1;  //A structure is nested within a structure
};

int main()
{
	//Struct variable access members -- using the (.) operator
	struct Point p1 = { 10, 20, {"cyuyan", "lisi", 40.0f, "1110001011"} };
	printf("%d %d %s %s %f %s\n", p1.x, p1.y, p1.b1.name, p1.b1.auther, p1.b1.price, p1.b1.id);

	//Structure pointers access members that point to variables -- using the (- >) operator
	struct Point* pp1 = &p1;
	printf("%d %d %s %s %f %s\n", (*pp1).x, (*pp1).y, (*pp1).b1.name, (*pp1).b1.auther, (*pp1).b1.price, (*pp1).b1.id);
	printf("%d %d %s %s %f %s\n", pp1->x, pp1->y, pp1->b1.name, pp1->b1.auther, pp1->b1.price, pp1->b1.id );
	return 0;
}

3, Structural transmission parameters

We know that parameter passing includes value passing call and address passing call, and the same is true for structure parameter passing.

struct S
{
	int data[1000];
	int num;
};
struct S s = { {1,2,3,4}, 1000 };
//Structure parameter passing (value passing call)
void print1(struct S s)
{
	printf("%d\n", s.num);
}
//Structure address transfer parameter (address transfer call)
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 above print1 (value transfer call) and print2 (address transfer call) functions is better?
The answer is: the print2 (address calling) function is preferred.
reason:
When a function passes parameters, the parameters need to be pressed on the stack.
(value passing call) 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.
(address calling) if an address is passed, the size of the address is fixed (4 / 8 bytes).
Conclusion:
When a structure passes parameters, the address of the structure should be passed.

Posted by veluit06 on Fri, 01 Oct 2021 20:03:06 -0700