当前位置:网站首页>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 .
边栏推荐
- [harmonyos] [arkui] how can Hongmeng ETS call pa
- PAT B1063
- Read multiple associations from a field using delimiters in laravel
- Log in to Huawei game with a minor account, and pop up anti addiction prompt after startup
- PAT B1053
- 200 OK (from memory cache) and 200 OK (from disk cache)
- Number of wechat applet custom input boxes
- Determine whether it is a web page opened on wechat
- JS asynchronism (I. asynchronous concept, basic use of web worker)
- String since I can perform performance tuning, I can call an expert directly
猜你喜欢

String since I can perform performance tuning, I can call an expert directly

Wechat applet swiper simple local picture display appears large blank

Li-rads lesion classification reading notes

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

<C>. tic-tac-toe

Using flex to implement the Holy Grail layout is as simple as that

Wechat applet connects to the server to display mqtt data information

Vulnhub range - the planes:venus

Arduino read temperature

Avoid material "minefields"! Play super high conversion rate
随机推荐
Est - il sûr d'ouvrir un compte avec de nouvelles dettes? Une faible Commission est - elle crédible?
Alicloud centos8.0 installing mysql8
New features of php7
Pcl+vs2019 configuration and some source code test cases and demos
LNMP compilation and installation
Simple native JS tab bar switching
Appearance of object attributes
Determine whether it is a web page opened on wechat
Go language installation and uninstallation
Hdoj topic 2005 day
PAT B1059
Web container basic configuration
Corporate finance formula_ P1_ Accounting statement and cash flow
Jsonp function encapsulation
Is it safe to open a new bond? Is low commission reliable
PHP FPM, workman, spoole, golang simple performance test
PAT B1096
Huawei in application review test requirements
Native JS array some method de duplication
Is it safe to open a new bond account