当前位置:网站首页>C language pointer (Part 1)
C language pointer (Part 1)
2022-07-07 09:06:00 【A big cat 1201】
author : A big cat 1201
special column :《C Language learning 》
Maxim : You just try to , Leave the rest to time !
C Language pointer
What is the pointer ?
C I believe everyone has heard of the pointer in language , So what exactly is it ?
Let's look at a picture :
There are many frames in this picture , It's like a building , One layer after another , It is memory , It stores all kinds of data . And each floor is numbered , It's like a house number , Different house numbers are the addresses of different memory spaces , Through the address, we can find the data in the corresponding memory space . What is the size of a memory space ?
With 32 For example, a bit machine :
32 The machine of the bit has 32 Root address line (64 The machines of bit have 64 Address lines ), Each address line has two levels , High level and low level , For high level 1 Express , For low level 0 Express .
that 32 individual 0 and 1 The combination of is 232 Kind of .
The size of each space is 1 Bytes B, So you need
232B÷210=222KB
222KB÷210=212MB
212MB÷210=4GB
Only need to 4GB The size of the space can be used for addressing .
You guys can try when the size of a memory space is not 1 How much space is needed to compile when the number of bytes is , You'll find that , Only one memory space size is 1 Bytes is the most reasonable .
By counting and analyzing appeals , We know that the size of a memory space is 1 Bytes , Such distribution will also be very convenient to use .
We have learned about memory , So what exactly is the pointer ?
Pointer is used to store the address of memory space , It stores the number of the smallest unit of memory .
Be careful :
- Strictly defined , The address is the pointer , Pointer variables are used to store addresses , That is, pointer variables are used to store pointers .
- The pointer we usually use in spoken language actually refers to pointer variables .
After knowing what the pointer is , So how does it work ?
int a = 10;
int* p = &a;
printf("%p\n", p);
By the operator &, Will create the int Variable of type a Get the address of , Then assign a value to the pointer variable p, We can find the data in the corresponding memory space through the address in the pointer variable .
The size of the pointer variable
Let's take a look at the variables in the above program a The address of
Its address is 0x010FFC24, This is because 16 Binary sequence in hexadecimal form , By 32 individual 0 and 1 Composed of
And this 32 Bits require 4 Only a space of bytes can fit , So the size of the pointer variable is 4 Bytes .
stay 32 On the machine , The size of the pointer variable is 4 Bytes , And in the 64 The size of the pointer variable on the bit machine is 8 Bytes .
Pointers and pointer types
We know , There are different types of variables , Yes int type ,char type ,double type …, Do pointer variables have types ? The answer is yes
When we create several variables :
int a = 10;
char b = 20;
float c = 3.14;
double d = 2.456;
We know , Different variable types have different sizes , That is, the number of bytes it occupies in memory is different .
If the pointer variable has no type , When we access the data pointed to by the address in the pointer variable, we won't know what kind of data it is , At this point, the compiler is messy , It doesn't know how many bytes of data to fetch .
So let the compiler understand , What type of data is put in the memory space pointed to by the address in the pointer variable .
int* pa = &a;
char* pb = &b;
float* pc = &c;
double* pd = &d;
Creation of pointer variables :
take int* pa = &a; For example
This statement can be written as int * pa = &a;
The left side of the equal sign can be divided into 3 Parts of ,int,* Number ,pa
* Sign means that this is a pointer variable ,pa Is the name of the pointer variable ,int Is indicating that the data type pointed to by this pointer variable is int Type of .
The meaning of pointer type
It is known that pointer variables are of type , So what is the meaning of its existence ?
int main()
{
int a = 10;
int* pa = &a;
char* p = (char*)&a;// Put the variable a The pointer of is cast to char Pointer variable of type
printf("&a = %p\n", &a);
printf("pa = %p\n", pa);
printf("pa + 1 = %p\n", pa + 1);
printf("p = %p\n", p);
printf("p + 1 = %p\n", p + 1);
return 0;
}
We create a variable a, Take out its address , We separate int Pointer variables of type and char In the pointer variable of , Then add 1 operation .
You can see ,int Pointer variable of type is adding 1 after , Its address has been increased 4 Bytes , and char Pointer variable of type is adding 1 after , Its address has only increased 1 Bytes , It is also variable a The address of , Why are you adding 1 The results are different ?
We draw the layout of the memory horizontally , On the left is the low address , On the right is the high address , Variable a The value of is stored in memory .
Pointer to the variable pa and p All points to variables a The address of , As shown in the above figure, the green arrow and the blue arrow .
Add these two pointer variables 1 After the operation
Because the pointer variable pa yes int Type of , At this point, the compiler thinks that the data it points to is int Type of , The size is four bytes , So will pa+1 Then it skips a int type , That is to say 4 Bytes , Point to the next int Type data .
Because the pointer variable p yes char Type of , At this point, the compiler thinks that the data it points to is char Type of , The size is one byte , So will p+1 Then it skips a char type , That is to say 1 Bytes , Point to the next char Type data .
The operation of adding and subtracting integers is the same , Nothing more than skipping several types of data pointed to by pointer variables .
So we say , The meaning of pointer variable type is : The type of pointer variable determines the step size of pointer variable access .
Next, we dereference the above pointer
int main()
{
int a = 0x11223344;
int* pa = &a;
char* p = (char*)&a;// Put the variable a The pointer of is cast to char Pointer variable of type
printf("*pa = %x\n", *pa);
printf("*p = %x\n", *p);
return 0;
}
We create a variable a, It's in there 16 Hexadecimal Numbers 11223344.
Code run results
We can see , Yes int The result of dereferencing pointer variables of type is 11223344, Yes char The result of a pointer variable of type followed by a reference is 44, Why is that ?
because VS2019 It is a small end byte order storage mode ( Please move to The storage of data in memory ), So what he looks like in memory is
At this time, the pointer variable pa and p All points to variables a The address of
When it comes to pointer variables pa and p When dereferencing
Pointer to the variable pa yes int type , The compiler thinks pa The address pointed to is int Data of type , Size is 4 Bytes , So when dereferencing access , To visit is a int type , That is to say 4 Contents of bytes of memory space . So the result is 11223344
Pointer to the variable p yes char type , The compiler thinks p The address pointed to is char Data of type , Size is 1 Bytes , So when dereferencing access , It is a char type , That is to say 1 Contents of bytes of memory space . So the result is the content of the first byte space , That is to say 44
So we say , The meaning of pointer variable type is : The type of pointer variable determines the amount of memory space accessed when understanding references .
summary :
Pointer variable types have two meanings
- The pointer variable type determines the step size of the pointer
- The type of pointer variable determines the amount of memory space accessed when understanding references
Wild pointer
Concept : The position of the pointer is unknown ( Random 、 incorrect 、 There is no definite limit to )
The origin of wild pointer
- Pointer not initialized
int main()
{
int* p;
*p = 20;
return 0;
}
The pointer variable at this time p It's not initialized , So its value is random , That is, the address it points to is random .
- Pointer cross boundary access
int main()
{
int arr[10] = {
0};
int* p = arr;
int i = 0;
for (i = 0; i <= 12; i++)
{
*(p++) = i;
}
return 0;
}
When... In the array 10 After the elements are assigned , The loop has not yet exited , When a pointer variable p Point to 11 When it's an element , At this time, the pointer variable p Just crossed the border , The pointer variable at this time p It's a wild pointer .
- The space that the pointer points to is released
int* test()
{
int a = 0;
return &a;
}
int main()
{
int* p = test();
printf("hehe\n");
printf("%d\n", *p);
return 0;
}
Running results
At this point, for pointer variables p The value after dereference is 5, It's not in test Function a.
This is because test Variables created in function a After the function is executed , Just put the variable a The created memory space is freed .
In the main function pintf(“hehe”) Statement changes the original variable a The space is covered .
Therefore, the original variables cannot be accessed after dereferencing a.
The pointer variable at this time p It's a wild pointer .
Methods to avoid wild pointers
- Pointer initialization
- Watch out for the pointer
- After the space pointed to by the pointer is released , To set it immediately NULL
- Avoid returning the address of a local variable
- Check the validity of the pointer before using it
int main()
{
// When you don't know the initialization value of the pointer variable , To initialize to control in NULL
int *p = NULL;
int a = 10;
p = &a;
// Check the effectiveness
if(p != NULL)
{
*p = 20;
}
return 0;
}
The operation of the pointer
Pointer variables can be operated like other variables .
- Pointer plus minus integer operation
int main()
{
int arr[] = {
1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(arr) / sizeof(arr[0]);
int* p = NULL;
for (p = arr; p < arr + sz;)
{
printf("%d ", *p);
p++;
}
return 0;
}
Running results
The above program is through pointer variables p Add 1 To control the progress of the cycle , Add and subtract integers and add 1 It's the same thing .
- Pointer minus pointer
#include <stdio.h>
#include <string.h>
int main()
{
char arr[] = "abcdef";
char* start = arr;
char* end = arr;
while (*end != '\0')
{
end++;
}
printf("strlen(arr) = %d\n", strlen(arr));
printf("end - start = %d", end - start);
return 0;
}
Running results
You can see , Pointer to the variable end Subtract the pointer variable start The value of is the same as the length of the string in the array .
Pointer to the variable start Pointing to ’a’
Pointer to the variable end The final point is ’f’, The essence of it is start+6
So the result of subtraction is 6
The result of subtracting pointer variables from pointer variables is the number of elements between two pointer variables .
- The relational operation of pointers
int main()
{
int arr[] = {
1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(arr) / sizeof(arr[0]);
int* p = arr;
for (; p < arr + sz; p++)
{
*p = 0;
}
return 0;
}
The above code is an array arr All ten elements in are set to 0.
among , Through pointer variables p And the address of the last element in the array to control the size of the loop .
You can see ,arr+sz It's crossed the border , The address is an array arr The first one in the back int The address of a type variable , Pointer to the variable p It refers to the address to control the cycle .
The above program is set one by one from low to high 0 Of , Then we can also set it from high to low 0
int main()
{
int arr[] = {
1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(arr) / sizeof(arr[0]);
for (int* p = &arr[sz - 1]; p >= &arr[0]; p--)
{
*p = 0;
}
return 0;
}
This sets the elements in the array from high address to low address 0 Code for .
Final pointer variable p Will point to the array arr The address of the first element in front , At this time, the control conditions are not met , Out of the loop
Although this is possible with most compilers , But try to avoid writing like this , Because it doesn't fit C Language standards
because C Language standards :
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 .
边栏推荐
- Original collection of hardware bear (updated on May 2022)
- PMP Exam details after the release of the new exam outline
- 【Istio Network CRD VirtualService、Envoyfilter】
- Simple use of Xray
- ESP32-ULP协处理器低功耗模式RTC GPIO中断唤醒
- Personal deduction topic classification record
- OpenGL帧缓冲
- Esp32-ulp coprocessor low power mode RTC GPIO interrupt wake up
- [MySQL] detailed explanation of trigger content of database advanced
- Explain Huawei's application market in detail, and gradually reduce 32-bit package applications and strategies in 2022
猜你喜欢
LeetCode 736. Lisp 语法解析
Reflections on the way of enterprise IT architecture transformation (Alibaba's China Taiwan strategic thought and architecture practice)
Panel display technology: LCD and OLED
阿里p8推荐,测试覆盖率工具—Jacoco,实用性极佳
Recommended by Alibaba P8, the test coverage tool - Jacobo is very practical
Ppt template and material download website (pure dry goods, recommended Collection)
Unityshader introduction essentials personal summary -- Basic chapter (I)
Systick滴答定时器
How long does the PMP usually need to prepare for the exam in advance?
串口实验——简单数据收发
随机推荐
平台化,强链补链的一个支点
Uniapp wechat applet monitoring network
Reflections on the way of enterprise IT architecture transformation (Alibaba's China Taiwan strategic thought and architecture practice)
Several stages of PMP preparation study
Systick滴答定时器
[chaosblade: node CPU load, node network delay, node network packet loss, node domain name access exception]
C语言指针(习题篇)
How to pass the PMP Exam in a short time?
硬核分享:硬件工程师常用工具包
Calculation s=1+12+123+1234+12345 C language
Golang etcdv3 reports an error. The attribute in grpc does not exist
Markdown编辑器Editor.md插件的使用
Esp32-ulp coprocessor low power mode RTC GPIO interrupt wake up
Full link voltage test of the e-commerce campaign Guide
[chaosblade: node disk filling, killing the specified process on the node, suspending the specified process on the node]
Ppt template and material download website (pure dry goods, recommended Collection)
模拟卷Leetcode【普通】1609. 奇偶树
Unity Shader入门精要初级篇(一)-- 基础光照笔记
[MySQL] detailed explanation of trigger content of database advanced
External interrupt to realize key experiment