当前位置:网站首页>Understanding C language structure pointer
Understanding C language structure pointer
2022-06-25 20:08:00 【Fu Zongheng】
Novice understanding c Language pointer
void changeI(int i) {
i = 25;
printf("changeI:%d\n", i);
}
void changeI(int *p) {
*p += 2;
printf("changeI:%d\n", *p);
}
void changeP(int *p) {
p = NULL;
printf("changeP:%d\n", p);
}
int main()
{
int i = 21, j = i;
int *p1 = &i, *p2 = &i;
*p1 = 23;
printf("i:%d\n", i);
printf("j:%d\n", j);
printf("p1:%d\n", *p1);
printf("p2:%d\n", *p2);
changeI(i);
printf("i:%d\n", i);
changeI(&i);
printf("i:%d\n", i);
changeI(p1);
printf("i:%d\n", i);
changeP(p2);
printf("p2:%d\n", p2);
}
As shown above :
The code is for this example only , Do not correct the file structure , Function declaration .
Output from the main function from top to bottom :
First four outputs :
- i = 21, j = i; Is a common assignment statement , Namely variable j The value of is now 21.
- *p1 = &i, *p2 = &i; Now? p1,p2 Are all i The address of .
- *p1 = 23; It is easy to understand in the main function , take p The value of the address pointed to is changed to 23, and p1 The value of itself has not changed ,p2 And p1 The value of is the same , Both point to variables i Address in memory , The value of this address is now changed to 23, namely i The value of is now 23.
- The first three correspond to four printf().
- first changeI in , We introduced i, The system passes in value , That is, what is passed in is not a variable i, It's a variable i Value , Is passed in 23; In function i Is a function local variable , The value is 23, therefore +=2 obtain 25, Then output 25;
- Back to the main function , Once again, the output i, In the main function i No change .
- the second changeI, Pass in i The address of , In this case, the passed in variable corresponds to the variable in the main function i, Therefore, the... In the main function is changed in the function i, Both outputs are 23+=2 Result ;
- And then changeI, Pass in p1 Value , That is, the variable is passed in i The address of , ditto , Output twice 27.
- changeP in , We introduced p2 Value , namely i The address of , Then assign this value to NULL, Be careful :p2 Only i The address of , Its worth of change does not lead to i The address has changed , therefore i And p1 All for receiving the image , And p2 For reasons worthy of transmission , Also for the impact . The last two output values are NULL And i Get address .
Think about how to change in a function p2 Value
We should have p2 The address is passed into the function .p2 Recorded i Get address , But it is also a variable , Also in memory , There are also addresses .
Thinking will int The type becomes a structure
If we define a pointer , Without defining variables , You want to assign a value to the area indicated by the pointer , You need to apply for space first .
typedef struct MyStruct
{
int ID;
char name[7];
int age;
} stu1;
void setValue(stu1 stu, int ID, char name[7], int age) {
stu.ID = ID;
for (int i = 0; i < 7; i++) {
stu.name[i] = name[i];
}
stu.age = age;
}
void setValue(stu1 *stu, int ID, char name[7], int age) {
(*stu).ID = ID;
for (int i = 0; i < 7; i++) {
(*stu).name[i] = name[i];
}
stu->age = age;
}
void printStu(stu1 stu) {
printf("ID:%d\t", stu.ID);
printf("name:%s\t", stu.name);
printf("age:%d\n", stu.age);
}
int main()
{
stu1 *st = (stu1*)malloc(sizeof(stu1));
stu1 stu;
char name[] = " Zhang San ";
stu.ID = 2000;
setValue(stu, 2000, name, 18);
printStu(stu);
char name1[] = " Li Si ";
setValue(st, 2001, name1, 21);
printStu(*st);
char name2[] = " Zhang San ";
setValue(&stu, 2000, name2, 18);
printStu(stu);
printf("");
}

- A structure pointer is created st, And applied for space
- Created a structure variable stu
- Trying to stu Call the assignment function , Note that the parameters in this example are different , Distinguish function , Like int type , We passed stu Is treated as the value passed into the structure , The structure itself cannot be modified , So the output function is garbled , If not stu.ID assignment , Then the system reports an error that it is not initialized .
- Pass in the structure pointer variable , Successfully modified the value pointed to , among (*stu) Represents the structure pointed to by the pointer ,stu-> Variables in the structure that can be found .
- Pass in stu The address of , The same as above .
Only the following points need to be paid attention to when using pointers :
- When defining variables ( Including the parameter definition of the function ):"*" Means to define a pointer type variable , Such as int *i;
- During code execution : The variable name indicates a pointer such as i;"* Variable name " Indicates the class that the pointer points to, such as * i;"&" Address , Such as &(*i) Same as i.int j=0;&j It is j The address of ,**&i It is int Pointer type variables i The address of ( Address of pointer variable / The address of the address )** You want to modify the pointer variable in the function , Also need to send its address .
边栏推荐
- 3、 Hikaricp source code analysis of connection acquisition process III
- 2.4 finding the sum of the first n terms of the interleaved sequence
- PAT B1051
- 2.3 partial sum of square and reciprocal sequences
- [harmonyos] [arkui] how can Hongmeng ETS call pa
- Vulnhub range - correlation:2
- PAT B1066
- Matlab_ two point zero five
- 通过启牛学堂开的股票账户可以用吗?资金安全吗?
- From now on, I will blog my code
猜你喜欢

2020-12-09 laravel . Env file loading mechanism process

2.14(Knight Moves)

2.3 partial sum of square and reciprocal sequences

Transunet reading notes

Arduino ide + esp8266+mqtt subscribe to publish temperature and humidity information

206. reverse linked list (insert, iteration and recursion)

<C>. Figure guessing game

Profile path and name

H5 application conversion fast application

Pcl+vs2019 configuration and some source code test cases and demos
随机推荐
PAT B1056
Huawei HMS core launched a new member conversion & retention prediction model
Applet password input box
Corporate finance formula_ P1_ Accounting statement and cash flow
JS get the parameters in the URL link
Install spoole
Simple native JS tab bar switching
Pta--7-20 exchange minimum and maximum (15 points)
Huawei fast application access advertising service development guide
Redis practice: smart use of data types to achieve 100 million level data statistics
在打新債開戶證券安全嗎?低傭金靠譜嗎
PAT B1059
Vulnhub range the planes:earth
Li-rads lesion classification reading notes
Swin UNET reading notes
Can the stock account opened through qiniu school be used? Is the fund safe?
PAT B1071
Single chip microcomputer line selection method to store impression (address range) method + Example
Vulnhub range - the planes:venus
<C>. Calculation date to day conversion