当前位置:网站首页>[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 .
边栏推荐
- Find the memory occupied by the structure
- Leetcode Day5 delete duplicate email
- Implementation of strstr in C language
- Using Lex (Flex) to generate lexical analyzer of PL language
- Serial port receiving application ring buffer
- 克服“看牙恐惧”,我们用技术改变行业
- Return and job management of saltstack
- [C language] header file of complex number four operations and complex number operations
- Handan, Hebei: expand grassroots employment space and help college graduates obtain employment
- Labelme (I)
猜你喜欢

Codeignier framework implements restful API interface programming

冲刺金九银十丨熬夜半个月汇集大厂Android岗1600道面试真题

High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation

11. Learn MySQL union operator

Edge detection and connection of image segmentation realized by MATLAB
![[C language] print pattern summary](/img/48/d8ff17453e810fcd9269f56eda4d47.png)
[C language] print pattern summary

Implementation of markdown editor in editor.md

通信网络基础知识01

There is a 'single quotation mark' problem in the string when Oracle inserts data

How to write the SQL statement of time to date?
随机推荐
数字滤波器设计——Matlab
English Translation Spanish - batch English Translation Spanish tools free of charge
软考高级考试中有五大证书,哪个更值得考?
There are five certificates in the soft examination advanced examination, which is more worth taking?
leetcode day1 分数排名
河北邯郸:拓展基层就业空间 助力高校毕业生就业
C language function
MySQL8 Status Variables: Internal Temporary Tables and Files
Left alignment function of Lua language (handwriting)
Concurrent programming, do you really understand?
leetcode day3 查找重复的电子邮箱
Basic usage of docker
C language operators and input and output
XOR operation and its usage
软考中级(系统集成项目管理工程师)高频考点
爬取IP
This customized keyboard turns me on~
云计算笔记part.1——系统管理
MySQL performance testing tool sysbench learning
Sequential linear table - practice in class