当前位置:网站首页>Embedded-c language-6
Embedded-c language-6
2022-07-05 17:04:00 【Orange peel does not stop school】
One 、 The pointer (C Language soul )
1.1. Definition of pointer : A pointer is essentially a variable , and This variable can always store only one memory address ( Number ) Therefore, the technical term corresponding to this variable is called pointer variable , The memory area can be accessed arbitrarily through the address saved by the pointer variable ( Read view , Write and modify ), The memory area pointed to by the pointer can hold a number , And this number has a data type
1.2. Syntax format of pointer variable definition :
a) Writing form 1:
int * Variable name ;
for example :int * pa; // Define a pointer variable
b) Writing form 2:
int* Variable name ;
for example :int* pa; // Define a pointer variable
c) Writing form 3:
int *pa; // Define a pointer variable
semantics : Is to define a pointer variable , In the future, this variable pa Can save the first address of a memory area , And this memory area holds a int Data of type , Because pointer variables are also variables , You also need to allocate memory space
ask : How much memory space does the pointer variable occupy ?
answer : It depends on the size of the saved address , This is related to computer hardware :
32 Bit system , An address value 32 position ,4 byte
64 Bit system , An address value 64 position ,8 byte
Conclusion : The memory space allocated by the pointer variable is 4 Byte or 8 byte , So the pointer variable itself has no data type , Only the number stored in the memory area it points to has data types , therefore int Not for pointer variables , Instead, it is used to store the number in the memory area pointed to by the pointer variable
d) Continuously define pointer variable form :
int *pa, *pb; // Define two pointer variables
int *pa, pb; //pa It's a pointer variable , and pb It's just an ordinary int Type variable
e) Bear in mind : After defining the pointer variable If not initialized , This pointer variable saves a The address value is random Of , That is, this pointer variable points to any memory region , It's quite dangerous , Because this block of memory area , Not the memory legally allocated to you by the operating system , This pointer variable is a wild pointer !
1.3. Pointer variables are initialized by fetching addresses & To carry out :
int a = 250; // Distribute 4 Byte memory space , Storage 250 Numbers , The number type is int type
int *pa = &a; // Define a pointer variable , That is to assign one 4 Byte memory space ( Premise is 32 position )
Save variables a The first address of the corresponding memory space , Be commonly called pa Point to a
ask : Once the first address of the memory region pointed to is obtained through the pointer variable , How to pass pointer variables , Operate on the memory area pointed to , That is, read view or write modify the memory area pointed to ?
answer : Through the dereference operator :*
1.4. Dereference operator ( Also known as the target operator ):*
function : This is to read, view, or write modify the memory area pointed to through the pointer variable
Grammar format :* Pointer to the variable = Take the target
for example :
char a = 100;
char *pa = &a;
perhaps :
char a = 100;
char *pa = NULL;
pa = &a;
// Print pa Point to a Memory data for
printf("%d\n", *pa); //100
// modify pa Point to a Memory data for
*pa = 10; // The result is a variable a The memory is changed from the original 100 become 10
Conclusion :sizeof( Pointer variable name ) = 4( Forever )
1.5. Special pointer : Null pointer and wild pointer
a) Null pointer : A null pointer variable holds a null address , use NULL Express , In fact, it is numbered 0 Address
Null pointers are not freely accessible , Otherwise, the program will crash !
for example :
int *pa = NULL;
printf("pa Point to the 0 The data saved by the address is %#x\n", *pa);
*pa = 250; // towards 0 Address write data 250
b) Wild pointer : Uninitialized pointer variables , It holds a random address , It points to an invalid memory , Because this memory area is not allocated to you by the operating system , If the wild pointer is accessed illegally , It will also cause the program to crash !
int *pa; //pa It's a wild pointer
printf("pa Point to the 0 The data saved by the address is %#x\n", *pa);
*pa = 250; // towards 0 Address write data 250
c) Bear in mind : The programming specification of the actual development code
If you define a pointer variable , At first it was not clear who it was pointing to , Never fail to initialize , Otherwise, it becomes a wild pointer, so it is required to initialize to a null pointer at this time NULL, Once initialized to NULL, When used later in the program , Remember to make safe judgments about pointer variables , Judge whether it is NULL, If NULL, Let the program end or the function return , If it is a valid address , The program can continue to operate through pointer variables
for example :
int *pa; // It's not recommended to write , dangerous
// Safe practices :
int *pa = NULL; // The assignment pointer is null
if(NULL == pa) {
printf("pa Pointer to null , Cannot continue to access .\n");
return -1 perhaps exit(0); // Function return or program exit
} else {
printf("pa Point to a piece of valid memory , You can continue to visit \n");
printf("%d\n", *pa);
*pa = 250;
}
// Safe practices :
int *pa = NULL; // The assignment pointer is null
int a = 250;
pa = &a; // Give Way pa Point to a
if(NULL == pa) {
printf("pa Pointer to null , Cannot continue to access .\n");
return -1 perhaps exit(0); // Function return or program exit
} else {
printf("pa Point to a piece of valid memory , You can continue to visit \n");
printf("%d\n", *pa);
*pa = 251;
}1.6. Pointer arithmetic
a) A pointer can add or subtract from an integer , Address operation for short
Bear in mind : The calculation result is related to the variable data type pointed to by the pointer
b) Pointer calculation formula :
1.char Type pointer +1, Indicates the actual address +1
for example :
char *pa = 0x1000; // hypothesis pa Point to 0x1000
pa++; //pa=0x1001
2.short Type pointer +1, Indicates the actual address +2
short *pa = 0x1000; // hypothesis pa Point to 0x1000
pa++; //pa=0x1002
3.int/long Type pointer +1, Indicates the actual address +4
long *pa = 0x1000; // hypothesis pa Point to 0x1000
pa++; //pa=0x1004
1.7. Pointers and arrays ( We used to study pointers and variables )
a) Review the array
Define an array :int a[4] = {'A', 'B', 'C', 'D'};
Conclusion :
1. The array name is the first address of the array , Also follow the operation formula of the pointer
2.&a[2] That is the first. 2 The first address of an element
3.a+2 Also the first 2 The first address of an element
4.&a[2] - a = a + 2 - a = 2 Elements , It means the first one 2 Elements and number 0 Difference between elements 2 Elements , The actual address difference 8 Bytes =2 Elements *int
5. The goal is :a,&a[2],a+2 They're all addresses , Simply define a pointer variable to save the first address of the array , In the future, it is easy to access elements by using pointer variables and calculation formulas of pointer variables
b) Pointer and array relation formula :
int a[4] = {1,2,3,4,5};
int *pa = a; // Define a pointer variable to hold the first address of the entire array
1. How to write pointers and arrays 1:
// View the values of all elements :
for(int i = 0; i < sizeof(a)/sizeof(a[0]); i++)
printf("%d\n", *(p+i));
// Multiply all elements 10 times
for(int i = 0; i < sizeof(a)/sizeof(a[0]); i++)
*(p+i) *= 10;2. How to write pointers and arrays 2:
int len = sizeof(a)/sizeof(a[0]) ; // Find the memory space size of the array
// View the values of all elements :
for(pa = a; pa < a + len; p++)
printf("%d\n", *p);
// Multiply all elements 10 times
for(pa = a; pa < a + len; )
*p++ *= 10; //*p++: First calculate *p, Do later p++3. How to write pointers and arrays 3:
// View the values of all elements :
for(int i = 0; i < sizeof(a)/sizeof(a[0]); i++)
printf("%d\n", p[i]);
// Multiply all elements 10 times
for(int i = 0; i < sizeof(a)/sizeof(a[0]); i++)
p[i] *= 10;Bear in mind :"[]" Operator has to go through two steps :
for example :a[2] After two steps :
1. Calculate the address first :a + 2
2. Then take :*(a+2)
therefore :p[2] After two steps ;
1. Calculate the address first :p+2
2. Then take :*(p+2)
c) The ultimate formula to get the element value : a[i] = p[i] = *(a+i) = *(p+i)
1.8. Constant , Constant pointer , constant pointer , Constant pointer constant : Focus on the key const( The written examination questions are required )
a) Constant definition : Non modifiable values , for example :250,'A' etc.
b)const Keyword function : Constant quantity , Four forms :
1.const You can modify ordinary variables , Once the variable is modified, it will be treated as a constant , That is, the value cannot be changed after initialization
for example :
const int a = 250;
a = 200; //gcc Error will be reported at compile time
2. Constant pointer : The value of the memory region pointed to cannot be modified through the pointer variable ( Protect the memory area from random changes )
for example :
int a = 250;
const int *p = &a; // Definition initializes a constant pointer
perhaps
int const *p = &a; // Definition initializes a constant pointer
*p = 200; //gcc Error will be reported at compile time
printf("%d\n", *p); // Sure , Just read and see
perhaps
int b = 300;
p = &b; // Sure , Pointer to the variable p It can be modified ,p Now point to b
*p = 400; //gcc Error will be reported at compile time
printf("%d\n", *p);// Sure , Just read and see
3. constant pointer : The pointer always points to a memory area , Can't point to other places ( Protect the pointer from pointing randomly )
for example :
int a = 100;
int* const p = &a;
*p = 300; // Sure , You can modify the memory area pointed to
int b = 200;
p = &b; // Can not be ,gcc Report errors
4.const int * const p: Constant pointer constant , Express p It cannot be modified , meanwhile p The target cannot be modified Only through p To view the value of the memory area
for example :
int a = 100;
const int* const p = &a;
*p = 300; // Can not be , You can modify the memory area pointed to
int b = 200;
p = &b; // Can not be ,gcc Report errors
printf("%d\n", &p); // Sure
边栏推荐
- 精准防疫有“利器”| 芯讯通助力数字哨兵护航复市
- Writing method of twig array merging
- Application of threshold homomorphic encryption in privacy Computing: Interpretation
- Benji Bananas 会员通行证持有人第二季奖励活动更新一览
- 【机器人坐标系第一讲】
- [729. My Schedule i]
- Sentinel-流量防卫兵
- Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)
- Do sqlserver have any requirements for database performance when doing CDC
- 麻烦问下,DMS中使用Redis语法是以云数据库Redis社区版的命令为参考的嘛
猜你喜欢

Jarvis OJ Webshell分析

Wsl2.0 installation

阈值同态加密在隐私计算中的应用:解读

American chips are no longer proud, and Chinese chips have successfully won the first place in emerging fields

Deep learning plus

机器学习编译第2讲:张量程序抽象

Jarvis OJ 远程登录协议

Error in composer installation: no composer lock file present.

Benji Banas membership pass holders' second quarter reward activities update list

Android privacy sandbox developer preview 3: privacy, security and personalized experience
随机推荐
外盘期货平台如何辨别正规安全?
【剑指 Offer】61. 扑克牌中的顺子
[Jianzhi offer] 63 Maximum profit of stock
Combined use of vant popup+ other components and pit avoidance Guide
[brush questions] effective Sudoku
Sentinel flow guard
C# TCP如何限制单个客户端的访问流量
Copy mode DMA
Wechat official account web page authorization login is so simple
Machine learning compilation lesson 2: tensor program abstraction
Data verification before and after JSON to map -- custom UDF
tf. sequence_ Mask function explanation case
【729. 我的日程安排錶 I】
Is it safe to open a securities account by mobile phone? Detailed steps of how to buy stocks
Detailed explanation of use scenarios and functions of polar coordinate sector diagram
Scratch colorful candied haws Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
Benji Bananas 会员通行证持有人第二季奖励活动更新一览
How to uninstall MySQL cleanly
深耕5G,芯讯通持续推动5G应用百花齐放
Deep learning plus