当前位置:网站首页>[C language] Pointer elementary knowledge points
[C language] Pointer elementary knowledge points
2022-07-28 20:04:00 【An ran_】
List of articles
1. What is the pointer
(1) The pointer
The pointer is the number of the smallest unit in memory, that is, the address , Pointer in spoken language is generally pointer variable .
(2) Pointer to the variable
Pointer variables are variables used to store addresses , The values stored in this variable are treated as addresses .
(3) The size of the pointer
32 Bit platform (x86 In the environment ) Next 4 Bytes ,64 Bit platform (x64 In the environment ) Next 8 Bytes .
2. Pointers and pointer types
(1) Pointer definition method
Define the way :type+*
for example : Character :char *p=NULL;
plastic :int* p=NULL;
(2) The type of pointer
The type of pointer : Shaping the pointer 、 Floating point pointer 、 Character pointer 、 Array pointer 、 Function pointers, etc .【 What types of variables , The pointer has a corresponding pointer 】
(3) The meaning of pointer type
① Decide to perform the operation —— The pointer ± Integers , The number of bytes skipped determines the step size
② Determine the operation permission after pointer dereference —— You can change the number of a byte in memory
for example :char* The solution application of can only access one byte , and int* Dereference of can be accessed 4 Bytes ,double* Dereference of can be accessed 8 Bytes
· There are corresponding exercises in the follow-up
3. Wild pointer
(1) What is the wild pointer
A wild pointer is a pointer variable whose value is Illegal memory address , The wild pointer is not a null pointer (NULL).
(2) Wild pointer hazards
Wild pointer hazards : It can lead to Memory out of bounds 、 Segment error Other questions .
another : Legal memory addresses include the addresses of defined variables ,malloc The function requests the address to put the heap memory back, but it is not used free Release .
(3) Common avoidance of wild pointers
① Local variable initialization
If the global variable is not initialized, it will be automatically assigned 0, But local variables don't .
So when we define local pointer variables, we'd better use Local pointer variable initialization NULL, The local variable is initialized to 0.
② Do not return a local in a function / Formal variables and local / Address of formal array .
The address of the formal parameter in the function is after the function call 、 Pointer variables are destroyed immediately before assignment , So the address pointed to at this time is illegal .
③ After releasing the address pointed to by the pointer variable, assign the value of the pointer variable to NULL, Do not use the pointer that has been released
④ Avoid pointer operation errors
Improper operation of the pointer will cause the pointer to be a piece of memory that has been used by other processes , To avoid this situation , Make sure that the character array starts with '\0' ending , Their own use Memory function And the length information specified by the memory related function written , Prevent memory overruns .
⑤ Avoid incorrect cast
A common situation is , Under the multi-level dereference operation ,( for example :(int *)((int)a + 1)) take int Or the reshaped data mistakenly believes that an address is forcibly converted to a pointer type 【 The data stored in the pointer variable is essentially an integer 】, Lead to illegal space .
4. Pointer arithmetic
Not all operations on pointers are legal , In fact, there are very few types of operations that pointers can perform , It is mainly divided into the following two categories and three subspecies .
(1) The arithmetic operation of the pointer
C The pointer operation of is limited to the following two forms : The pointer ± Integers 、 The pointer - The pointer . And the former standard definition is only used to point to a pointer to an element in the array
① The pointer ± Integers
When a pointer and an integer perform arithmetic operations , It will always be adjusted according to the appropriate size . This suitable size is The size of the type pointed to by the pointer .
The result of the calculation is Still pointer .
// The first one is
float x[5]={
1,2,3,4,5};
float* p=&x[0];
p++;
// The second kind
double a[5]={
1,2,3,4,5};
double* pa=&a[0];
pa++;
here p++ Here is the pointer p Add 4 Bytes 【float The type is 4 Bytes 】,pa++ Here is the pointer pa Add 8 Bytes 【double The type is 8 Bytes 】
② The pointer - The pointer
Premise : One pointer is allowed only when both pointers point to elements in the same array - Another pointer 【 Assume that the results are meaningful 】.
The value of subtraction is the distance between two pointers in memory ( In terms of the length of an array element , instead of In bytes !!!)
therefore , The result of the calculation is It is of this type between two pointers Number of elements .
(2) The relational operation of pointers
Premise :<、>、>=、<= The operation requires that both before and after the operation point to the elements of the same array .
Common use scenarios
for(vp = &values[N_VALUES]; vp > &values[0];)
{
*--vp = 0;
}
another : Pointers to array elements can only point to elements inside the array , However, when the array is out of bounds due to incorrect operation , It is not allowed when the pointer points to the memory location before the first element of the array , The compiler will directly report an error —— Illegal access to memory , When the pointer points to the memory location after the last element of the array, it will not report an error , But the result Generally, it will be a random value ; Allows a pointer to an array element to be compared with a pointer to the memory location after the last element of the array , However, it is not allowed to compare with a pointer to the memory location before the first element .
The above results are the result of the joint action of standards and operating system memory access requirements .
5. Pointers and arrays
(1) Array name meaning
Array name meaning : It is essentially the address of the first element of the array , Except for two cases ——sizeof( Array name )【 Calculate the size of the entire array 】 as well as & Array name 【 Take the size of the entire array , The type is Array element type (*)[ Number ]】
(2) You can access the array through a pointer
int main()
{
int arr[] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int *p = arr; // The pointer holds the address of the first element of the array
int sz = sizeof(arr) / sizeof(arr[0]);
int i = 0;
for (i = 0; i<sz; i++)
{
printf("%d ", *(p + i));
}
return 0; }
6. The secondary pointer
Pointer variables are essentially variables , If it is a variable, it will have an address , A pointer whose content is the address of a pointer variable is a secondary pointer .
7. Pointer array
The essence is array . analogy : Shape array 、 Character array, etc , The type of array element in front , Next is the essence .
so : A pointer array is an array in which every array element is a pointer .
边栏推荐
- 你知道雨的类型有几种?
- Thoroughly understand bit operations - shift left, shift right
- Concurrent programming, do you really understand?
- Basic concept and essence of Architecture
- Common APIs in string
- Implementation of markdown editor in editor.md
- 克服“看牙恐惧”,我们用技术改变行业
- MySQL命令语句(个人总结)
- Servlet learning notes
- How many types of rain do you know?
猜你喜欢

“中国网事·感动2022”二季度网络感动人物评选结果揭晓

NetCoreAPI操作Excel表格
![[C language] function](/img/81/c185e2bb5eccc13433483f9558f52a.png)
[C language] function

Deploy LNMP automatically with saltstack

Concurrent programming, do you really understand?

基于QTGUI图像界面的空战游戏设计

时间转日期的sql语句应该怎么写?

Thoroughly understand bit operations - shift left, shift right

Use Hal Library of STM32 to drive 1.54 inch TFT screen (240*240 st7789v)

Cloud computing notes part.2 - Application Management
随机推荐
爬取IP
Leetcode day3 employees who exceed the manager's income
Failed to install app-debug. apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
Concurrent programming, do you really understand?
Tencent cloud deployment lamp_ Experience of building a station
利用STM32的HAL库驱动1.54寸 TFT屏(240*240 ST7789V)
通信网络基础知识01
基于C语言的信息管理系统和小游戏
软考高级考试中有五大证书,哪个更值得考?
[wechat applet development] page navigation and parameter transmission
河北:稳粮扩豆助力粮油生产提质增效
云原生编程挑战赛火热开赛,51 万奖金等你来挑战!
时间转日期的sql语句应该怎么写?
[C language] summary of methods for solving the greatest common divisor
Data system of saltstack
KPMG China: insights into information technology audit projects of securities fund management institutions
Digital filter design matlab
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
Cloud computing notes part.1 - system management
Machine learning -- model evaluation, selection and verification