当前位置:网站首页>Basic pointer ~ guide you to the introduction pointer
Basic pointer ~ guide you to the introduction pointer
2022-06-09 09:29:00 【I usually pass by banyuanjun】
What is a pointer ?
The pointer , Is the number of the smallest cell in memory , That's the address , The pointer we usually talk about orally is actually a pointer variable , That is, the variable used to store the address .
When we use sizeof To calculate the size of a pointer variable , Will find , No matter what type of pointer variable , It's the same size ,32 Bit platform yes 4,64 Bit platform is 8, This may cause people's doubts , Since the size of each pointer variable is the same , So what's the use of different types of pointers ? In fact! , The type of pointer determines how many bytes will be modified or changed during pointer calculation .
1: Pointer type determines when pointer variables are dereferenced , Number of bytes modified
such as int* It's four bytes ,char* It's one byte .
We give the following real column to make you understand it more quickly :
#include<stdio.h>
int main()
{
int a = 0x11223344;
char *pc = (char*)&a;
*pc = 0;
printf("%x",a);
return 0;
}
We can see , Only 44 Has been changed to 00, This is because 4 Bytes stored in base 16 Base number , and a Every two digits in a byte , also pc yes char* Type of , Only one byte can be accessed , therefore a Changed to 11223300;
2: The pointer type determines the addition and subtraction of the pointer , Can skip a few bytes
such as int* Pointer variable of type , When he +1 Informed -1 Will increase 4 Bytes or less 4 Bytes ;
But the pointer has another problem , That's the wild pointer problem ;
Wild pointer
Wild pointers are usually caused by the following problems :
1: Created a pointer variable without initializing
2: The range pointed to by the pointer variable exceeds the range of the array , Became a wild pointer
3: The address pointed to by the pointer is the address of the variable inside the function
Only the third point is difficult to understand , For your convenience , Here is a simple list :
#include<stdio.h>
int* test()
{
int a = 10;
return &a;
}
int main()
{
int* p = test();
return 0;
}here p A pointer variable points to a function a The address of , But we all know that , After the function is used up, its internal variables will be destroyed , therefore p The address pointed to by the variable is gone , therefore p Became a wild pointer .
Here are some tips to avoid wild pointers :
1: When the pointer is initialized , If there is no variable to point to , It's initialized to NULL
2: The pointer can only be dereferenced if it stores a valid address
3: Avoid using a pointer to the address of a local variable
4: Use the pointer to check the validity first
Pointer arithmetic
Pointer variables are also variables , Of course, the pointer also has its own way of operation ,
1: Pointers add and subtract integers
The pointer plus or minus the integer will move the address size of the corresponding byte , We can access the array directly in this way .
such as :
#include<stdio.h>
int main()
{
int arr[10] = { 0 };
int* p = arr;
for (int i = 0; i < 10; i++)
{
*(p + i) = 1;
}
for (int i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
} 
The result is as follows. .
2: Pointer minus pointer
The premise of subtracting pointer from pointer is that two pointers point to the same space , And the absolute value of subtracting two pointers is the number of two elements .
#include<stdio.h>
int main()
{
int arr[10] = { 0 };
int* p1 = arr;
int* p2 = (arr + 10);
printf("%d", p2 - p1);
return 0;
} 
3: Pointer relation operation ( Compare the size )
The premise is that two pointers must point to the same space ;
But one thing to note is , In the standard, it is allowed to compare the pointer to the array element with the address of the last element after the last element of the array , But it is not allowed to compare with the address of an element before the first element of the array
And arrays are closely related to pointers , The element name of an array is actually the address of the first element of the array , So you can use pointers to access arrays , As for how to access , It has been shown before and will not be shown again .
We talked about the first level pointer before , Of course, we also need a second level pointer , The concept of secondary pointer is very simple , Is the pointer that stores the first level pointer address .
Pointer variables can also be stored in arrays , An array that is all pointer variables is a pointer array ;
Pointer array
The form of pointer array is as follows
int * a[10];
a The array stores int* Variable of type .
边栏推荐
- 2022-2028 global online code learning industry research and trend analysis report
- MySQL basic addition, deletion, modification and query exercise
- three.js学习笔记(十六)——汹涌的海洋
- Qt development -- compilation of serial port assistant
- MySQL基础 数据库创建基础
- Can I LINQ a JSON- Can I LINQ a JSON?
- HVV blue team points north
- C指针复习
- redhat7 破解(重置)root密码
- [matlab] [digital simulation] [1] linprog solving linear programming, standard type
猜你喜欢

HVV blue team points north

neo4j实现社交推荐(四)

MySQL基础 数据类型精讲

Can I LINQ a JSON- Can I LINQ a JSON?

Kusionstack has a sense of open source | it took two years to break the dilemma of "separating lines like mountains"

测试不会被开发取代吗?
![Paper understanding [RL - exp replay] - an equivalence between loss functions and non uniform sampling in exp replay](/img/6f/4ec9e027a7c3c89aca1e5f66948289.png)
Paper understanding [RL - exp replay] - an equivalence between loss functions and non uniform sampling in exp replay
![Change exchange (boundary protection should work before array access) (if there is no such combination, dp[i] should also be integer.max_value - 1) leetcode 80](/img/9a/f525e1b065d1fd14d490505ad1525a.jpg)
Change exchange (boundary protection should work before array access) (if there is no such combination, dp[i] should also be integer.max_value - 1) leetcode 80

Implementation of WTM based on NETCORE framework

English grammar_ adverb
随机推荐
[redis learning 11] data persistence of distributed cache, master-slave cluster
将文件流(InputStream)写入文件 将上传文件MultipartFile写到文件
Creation of menu for wechat applet development
【Redis学习13】Redis搭建主从集群、哨兵集群、分片集群
dotnet core 也能协调分布式事务啦!
Paper understanding [RL - exp replay] - an equivalence between loss functions and non uniform sampling in exp replay
根据投影坐标裁剪影像中的目标区域(附有完整代码)
MySQL基础 子查询
如何使用阿里云 CDN 对部署在函数计算上的静态网站进行缓存
How to draw a picture gracefully
English grammar_ adverb
Codeworks round 797 (Div. 3) a~e problem solving record
MySQL基础 函数篇
[redis learning 13] redis builds master-slave cluster, sentinel cluster and partition cluster
如何看待 Dapr、Layotto 这种多运行时架构?
English grammar_ Adverb of place
Shell script connects 157 servers to the network in batches, configures password free, and resets the root password
数据库问题MySQL
FreeRtos任务通知复习
了解图数据库neo4j(二)