当前位置:网站首页>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
边栏推荐
- The first EMQ in China joined Amazon cloud technology's "startup acceleration - global partner network program"
- 【剑指 Offer】61. 扑克牌中的顺子
- Yarn common commands
- Solve cmakelist find_ Package cannot find Qt5, ECM cannot be found
- 浏览器渲染原理以及重排与重绘
- easyNmon使用汇总
- Benji Bananas 会员通行证持有人第二季奖励活动更新一览
- [brush title] goose factory shirt problem
- NPM installation
- JSON转MAP前后数据校验 -- 自定义UDF
猜你喜欢
How to install MySQL
拷贝方式之DMA
How to set the WiFi password of the router on the computer
浏览器渲染原理以及重排与重绘
如何将mysql卸载干净
Scratch colorful candied haws Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
Deeply cultivate 5g, and smart core continues to promote 5g applications
How was the middle table destroyed?
【刷題篇】鹅廠文化衫問題
[729. My Schedule i]
随机推荐
国产芯片产业链两条路齐头并进,ASML真慌了而大举加大合作力度
【剑指 Offer】63. 股票的最大利润
中国广电正式推出5G服务,中国移动赶紧推出免费服务挽留用户
解决CMakeList find_package找不到Qt5,找不到ECM
Excuse me, is the redis syntax used in DMS based on the commands of the redis community version of the cloud database
Deep dive kotlin synergy (XXI): flow life cycle function
PHP 严格模式
腾讯音乐上线新产品“曲易买”,提供音乐商用版权授权
Sentinel-流量防卫兵
【性能测试】全链路压测
Global Data Center released DC brain system, enabling intelligent operation and management through science and technology
Writing method of twig array merging
composer安装报错:No composer.lock file present.
[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details
PHP strict mode
什么是ROM
Jarvis OJ shell流量分析
项目引入jar从私服Nexus 拉去遇到的一个问题
通过proc接口调试内核代码
启牛商学院股票开户安全吗?靠谱吗?