当前位置:网站首页>Standard C language 89
Standard C language 89
2022-07-25 06:27:00 【Luo CS】
Twenty-two 、 The pointer
What is a pointer :
A pointer is a data type , Use it to define pointer variables ; Pointer variables store integer types , Represents the number of memory
Through this number, you can access the corresponding memory
Why use a pointer :
1. Functions are independent of each other , But sometimes you need to share variables
Parameter passing is one-way value passing ;
Global variables are prone to naming conflicts ;
Using arrays is troublesome , Additional transfer length is required ;
Although the namespaces between functions are independent , But the address space is the same , Pointers can solve the problem of shared variables .
2. Because the parameter transfer between functions is value transfer ( Memory copy ), For variables with a large number of bytes , The efficiency of value transmission is low , If
To pass the address of a variable, you only need to pass 4 or 8 Bytes .
3. Heap memory cannot be named , It is not like data,bss,stack Let the variable name be related to memory , Only pointer records can be used
The address of heap memory to access the corresponding memory
How to use the pointer :
Definition : Type name * Variable name _p;
int* num_p; int *num_p;
1. The usage of pointer variables is very different from that of ordinary variables , It is suggested to name it with p End with a distinction
2. The type of pointer indicates the address of what type of variable is stored , Determines the number of bytes that can be accessed through this pointer
3. One * Only one pointer variable can be defined
int* a,b,c;//a Is a pointer
int *a, *b, *c;// It's all pointers
4. Pointer variables are the same as ordinary variables, and the default value is random , It is usually initialized to NULL;
Be careful : The number of bytes accessed is determined by the type when the pointer is defined , It won't change later
assignment : Variable name _p = Address ;// It must be a meaningful and authorized address
Point to stack memory :
int num = NULL;
int* p = #
Point to heap memory :
int* p =malloc(4)
Quoting : * Variable name _p
Access the corresponding memory through the memory number recorded in the pointer variable
This process may produce segment errors , The reason is that there is an illegal memory number stored inside
Attention when using pointers :
Null pointer : The value is NULL The pointer variable of is called null pointer , Null pointer dereference is bound to produce segment errors
NULL Generally used as an error flag , When the return value of a function is a pointer type ,
have access to NULL As the return result of function execution error
How to avoid segment errors caused by null pointers ?
Use an unknown pointer to make a judgment first
if(NULL == p)
{
}
1. When the parameter of a function is a pointer , The pointer passed by others may be a null pointer
2. When the return value obtained from the function is a pointer type , A null pointer may be returned
Be careful :NULL In most systems 0.
Wild pointer :
Pointers to uncertain memory spaces are called wild pointers
Consequences of dereferencing the field pointer :
1. Everything is all right .( Point to normal )
2. Segment error .( Point to illegal )
3. Dirty data .( Point to other data )
Wild pointer is more harmful than null pointer , Because he can't judge , And it may be a hidden error , Not exposed for a short time
All wild pointers are made by programmers themselves ,
How to avoid generating wild pointers ?
1. Pointer definitions must be initialized .
2. Function does not return stack memory ( Local variables in functions ) Address .
3. After the memory pointed to by the pointer is released , Pointer variables should be set to null in time NULL
23 、 The operation of the pointer
Pointer variables store integers , Theoretically, all operators that can be used by integers can be used , But most operators don't work
The pointer + n; The pointer + Pointer type width *n <=> Forward n Elements
The pointer - n; The pointer - Pointer type width *n <=> Back off n Elements
The pointer - The pointer ; ( The pointer - The pointer )/ Pointer type width Calculate how many pointer elements are separated between two pointers
Twenty-four 、 The pointer and const( Nearby )
When a pointer is used as a function parameter to improve the efficiency of parameter transfer , The efficiency of parameter transmission is improved , however
There is a risk that variable sharing will be modified , have access to const Protect the memory pointed to by the pointer from being modified
const int* p; Protect the memory pointed to by the pointer from being modified ;
int const *p; ditto
int* const p; Protect pointer variables from modification
const int* const p; Neither the pointer variable nor the memory pointed to by the pointer can be modified
int const * const p; ditto
twenty-five 、 Pointer arrays and array pointers
Pointer array : It's an array , An array of pointer variables ( Array members are pointer variables of the same type )
type * arr[ length ] int* arrp[10] = {};
Array pointer : Is a pointer , Is a pointer to an array ,
type (*arrp)[ length ] int (*arrp)[10] = &arr;
hexacosa- 、 Array name and pointer :
Array names are special pointers
The array name is a constant , You can't change its value ; No storage space of its own , There is a mapping relationship between it and the first address of the array
Array name == & Array name
Pointer variables have their own storage space , It has a pointing relationship with the memory it points to
When the pointer variable points to the first address of the array , Pointers can be used as array names , Array names can also be used as pointers .
Array name [i]==*( Array name +i)
*(p+i) == p[i]
Be careful : When an array is used as a parameter of a function, it becomes a pointer , All lengths are lost .
twenty-seven 、 The secondary pointer
The second level is the pointer to the pointer . The storage pair is the address of the pointer variable
Definition : Type name ** Variable name _pp;
assignment : Variable name _pp = & Pointer to the variable ;
Quoting : * Variable name _pp <==> Pointer to the variable
** Variable name _pp <==> data <==> * Pointer to the variable
Be careful : When pointer variables are shared between functions , Pass the address of the pointer ( The secondary pointer )
Twenty-eight 、 A function pointer
The function name is an address , The function name represents the entry position of the function in the code segment
It's a pointer to a function , It stores the entry location address of the function in the code segment .
return type (*P)( type 1 Parameters 1,.....);
You can use function pointers , Pass a function as an argument to another function , This method is called callback function .
边栏推荐
- 都说ScreenToGif是GIF录制神器,却不知其强大之处远不在此
- New developments in Data Governance: what is the impact of the EU's Data Governance Research Report on China
- U-boot-1.1.6 transplant notes (beginner)
- It is said that screentogif is a GIF recording artifact, but I don't know that its strength is far from here
- Ceres solver version 1.14 and eigen3.2.9
- 健康打卡每日提醒累了?那就让自动化帮你---HiFlow,应用连接自动化助手
- Bug notes
- 2022 "strong country Cup" preliminary WP (with script and detailed process)
- Bubble sort code implementation
- JZ7 rebuild binary tree
猜你喜欢

C#控件开源库:MetroFramework的下载

C#开源控件MetroFramework Demo项目下载和运行

SAP FICO 第三节 BDC和LTMC导入S4财务科目

Keilc51 usage details (III)
![[datawhale202207] reinforcement learning: strategy gradient and near end strategy optimization](/img/4e/aabc603e47977503a4bcc5d07b4c61.png)
[datawhale202207] reinforcement learning: strategy gradient and near end strategy optimization

【transformer】DeiT

Bubble sort code implementation

在C# WinForms应用程序中安装,配置和使用MetroFramework

Quick sort code implementation

Unity animator animation and state machine
随机推荐
Dry goods | training AI model can't find data? Collect 20 selected open source communities!
Ida Pro novice tutorial
DOM event type
[QT] solve the problem of Chinese garbled code output from QT console
深度解析:2022年最火的商业模式链动2+1,是合法模式吗?
Scientific computing library numpy Foundation & Improvement (Understanding + explanation of important functions)
Multithreading programming under Win32 API
[C language] in depth understanding of pointers and arrays (phase I)
【transformer】DeiT
JVM tuning summary -xms -xmx -xmn -xss
Jstat command summary [easy to understand]
JZ7 rebuild binary tree
Interlocked atom access series of functions
ROI pooling and ROI align
Installation and configuration of automatic operation and maintenance management workers ansible
Baidu, Alibaba, Tencent, who fell first?
Pic16f877xa instruction system (assembly language)
Machine learning keras fitting sine function
[datawhale202207] reinforcement learning: strategy gradient and near end strategy optimization
【剑指Offer】模拟实现atoi