primary structure

1. Basic knowledge of structure

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

1.1 The role of the structure

In everyday life, many things we encounter are a complex object. It is not enough if we only use a single aspect to understand it, but the structure can do it. A structure can clearly describe a complex object in many ways.

For example: how do we describe a student?
Students: Name, gender, age, score, etc. to describe it.

2. Declaration of structure

struct tag //tag is the tag name, which can be changed according to requirements
{
	member-list; //member list
}variable-list; //variable list (global)

Columns such as describe a student:
Note: Here we are declaring a structure type and not creating a structure variable.

struct stu
{
	char name[20]; //Name
	char sex[20]; //gender
	int age; //age
	float score; //Fraction
};

2.1 Types of structure members

Members of structures can be scalars, arrays, pointers, or even other structures.

2.2 Definition and initialization of structure variables

We have declared the structure type above, and then we can use it to create structure variables.

Two ways to define structure variables

struct stu
{
	char name[20];
	char sex[20]; 
	int age; 
	float score; 
	//Method 1: Define the variable s2 while declaring the type
}s2 = { "Li Si", "male", 21, 65.5f };//Structure variable s2 (global variable)

//equivalent to
//struct stu s2 = { "Li Si", "Male", 21, 65.5f };

int main()
{
	//Party two:
	//create structure variable s (local variable)
	struct stu s = { "Zhang San", "male", 20, 95.5f };
	return 0;
}

Note: The difference between the two methods is that one is a global variable and the other is a local variable.

2.3 Structure nested structure definition and initialization

struct PP
{
	int b;
	float c;
};

struct P
{
	char a;
	struct PP s;
	double d;
};

int main()
{
	struct P p = { 'a', {25, 55.5f}, 65.5 };
	return 0;
}

3. Access to structure members

3.1. Structure variable access members

Members of structure variables are accessed with the dot operator (.). The dot operator accepts two operands.

struct stu
{
	char name[20];
	char sex[20];
	int age;
	float score;
};

void Print(struct stu s)
{
	//Struct variable . member
	printf("%s %s %d %.1f\n", s.name, s.sex, s.age, s.score);
}
int main()
{
	struct stu s = { "Zhang San", "male", 20, 95.5f };
	Print(s); //pass structure
	return 0;
}

3.2 Structural pointer access to members of variables

The structure pointer is accessed through —>.

void Print2(struct stu* ps)
{
	//printf("%s %s %d %.1lf\n", (*ps).name, (*ps).sex, (*ps).age, (*ps).score);
	//Struct pointer -> member
	printf("%s %s %d %.1lf\n", ps->name, ps->sex, ps->age, ps->score);
}
int main()
{
	struct stu s = { "Zhang San", "male", 20, 95.5f };
	Print2(&s); //call by address
	return 0;
}

3.3 Nested structure access

struct PP
{
	int b;
	float c;
};

struct P
{
	char a;
	struct PP s;
	double d;
};

int main()
{
	struct P p = { 'a', {25, 55.5f}, 65.5 };
	                            //Twice the . operator access is fine
	printf("%c %d %.1f %.1lf\n", p.a, p.s.b, p.s.c, p.d);
	return 0;
}

4. Structure parameter passing

struct stu
{
	char name[20];
	char sex[20];
	int age;
	float score;
};
void Print(struct stu s)
{
	//Struct variable . member
	printf("%s %s %d %.1f\n", s.name, s.sex, s.age, s.score);
}

void Print2(struct stu* ps)
{
	//printf("%s %s %d %.1lf\n", (*ps).name, (*ps).sex, (*ps).age, (*ps).score);
	//Struct pointer -> member
	printf("%s %s %d %.1lf\n", ps->name, ps->sex, ps->age, ps->score);
}
int main()
{
	struct stu s = { "Zhang San", "male", 20, 95.5f };
	Print(s);  //pass structure
	Print2(&s); //call by address
	return 0;
}

Which of the above print and print2 functions do you think is better?

The answer is: the print2 function is preferred.

Reason: When a function passes parameters, the parameters need to be pushed onto the stack. If a structure object is passed, the structure is too large, and the system overhead of pushing the parameters on the stack is relatively large, which will lead to a decrease in performance.

Summary: When passing parameters to a structure, it is recommended to pass the address of the structure.

Tags: C Algorithm C++

Posted by taskagent on Sun, 01 Jan 2023 02:37:12 +0530