当前位置:网站首页>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
边栏推荐
- 时间戳strtotime前一天或后一天的日期
- Scratch colorful candied haws Electronic Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
- Practical example of propeller easydl: automatic scratch recognition of industrial parts
- Benji Bananas 会员通行证持有人第二季奖励活动更新一览
- If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
- PHP 严格模式
- Twig数组合并的写法
- 如何安装mysql
- 兰空图床苹果快捷指令
- [brush title] goose factory shirt problem
猜你喜欢
Android 隐私沙盒开发者预览版 3: 隐私安全和个性化体验全都要
If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
Basic introduction to the control of the row component displaying its children in the horizontal array (tutorial includes source code)
国产芯片产业链两条路齐头并进,ASML真慌了而大举加大合作力度
Jarvis OJ Flag
SQL injection of cisp-pte (Application of secondary injection)
Global Data Center released DC brain system, enabling intelligent operation and management through science and technology
7.Scala类
Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)
PHP人才招聘系统开发 源代码 招聘网站源码二次开发
随机推荐
NPM installation
Jarvis OJ shell traffic analysis
Apple has abandoned navigationview and used navigationstack and navigationsplitview to implement swiftui navigation
How to uninstall MySQL cleanly
Combined use of vant popup+ other components and pit avoidance Guide
Iphone14 with pill screen may trigger a rush for Chinese consumers
Deeply cultivate 5g, and smart core continues to promote 5g applications
[brush questions] effective Sudoku
Cs231n notes (bottom) - applicable to 0 Foundation
[wechat applet] read the life cycle and route jump of the applet
兰空图床苹果快捷指令
Games101 notes (III)
养不起真猫,就用代码吸猫 -Unity 粒子实现画猫咪
The first EMQ in China joined Amazon cloud technology's "startup acceleration - global partner network program"
采用药丸屏的iPhone14或引发中国消费者的热烈抢购
【jmeter】jmeter脚本高级写法:接口自动化脚本内全部为变量,参数(参数可jenkins配置),函数等实现完整业务流测试
JSON转MAP前后数据校验 -- 自定义UDF
Jarvis OJ Telnet Protocol
【Web攻防】WAF检测技术图谱
【性能测试】全链路压测