1. Defining and using structure variables
Basic knowledge of structure
A structure is a collection of values , These values are called member variables . Each member of the structure can be a different type of variable .
Create your own structure type
Members of a structure can be scalars , array , Pointer , Even other structures .
struct Structure name
{ Member table column }:↓
be careful : The name of the structure type consists of a keyword struct And structure name ( for example struct Student) structural morphology The name is specified by the user , also called " Structure marker "(
structure tag), To distinguish it from other structure types . In the above structure declaration Student Is the structure name ( Structure marker )
Type name member name :
" Member table column " also known as " Domain table ", Each member is a domain in the structure . The naming rules of member names are the same as variable names
Declare the form of the structure
Initialization of structure
Code explanation :
struct point { int x; int y; }; p1; // Defining variables while declaring types p1 struct point p2;
// Define structure variables p2 struct stu { char name[20]; // name int age; // Age }; struct stu s = {
"lisi",25 };// initialization struct Node { int data; struct point p; struct Node* next; };
n = { 10,{4,5},NULL };// Nested initialization of structure struct Node n2 = { 20,{5,6},NULL };// Structure nesting initialization
2. Access to structure members
Structure variable access member Members of structural variables are passed through point operators Visited . The point operator accepts two operands . for example :
We can see s There are members in it name and age
How do we visit ?
struct S s; strcpy(s.name, "zhangsan");// use . visit name member s.age = 20;// use . visit age member
Struct pointers access members that point to variables Sometimes our variable is not a structure , It's a pointer to a structure . How do I access members . as follows :
3. Structural transmission parameters
Don't say much, just go to the code !
struct p { int data[900]; int n; }; struct p s = { {4,3,2,1},900 }; // Structural transmission parameters
void point1(struct p s) { printf("%d %d\n", s.data,s.n); } // Structure address transmission parameter void
point2(struct p* s1) { printf("%d %d\n", s1->data, s1->n); } int main() {
point1(s);// Transmission structure point2(&s);// Transmission address return 0; }
For the above two functions point 1 and point2 Which function is better ?
be the first choice point2 function
reason :
When passing parameters to a function , Parameters need to be stacked . If you pass a structure object , The structure is too large , The system overhead of parameter stack pressing is relatively large , So it will lead to performance degradation decline
conclusion :
When the structure passes parameters , Address of structure to be transferred . The above is Some preliminary versions sorted out by Xiao Wang for you Know the structure I'll give it to you later Bring an advanced version ( Liver pain .jpg) Difficult to make
I also hope you can give me more support It is the greatest progress and driving force for Xiao Wang !
Technology