当前位置:网站首页>[C language] summary of basic knowledge points of pointer
[C language] summary of basic knowledge points of pointer
2022-06-12 01:35:00 【MuShan-bit】
The pointer ( One ) Basics
One The concept of pointer
- To facilitate access to the contents of memory , Give each memory unit a number , We call this number address , And are pointers .
- A pointer is also a data type So pointers have their own memory What's stored is the address ( Number )
The four elements of the pointer
- The type of the pointer itself Remove the pointer name , The rest is the type of the pointer itself
- The type pointed to by the pointer Remove the pointer name and a *, The rest is the type pointed to by the pointer
- The memory of the pointer itself Used to store a number (4 byte )
- The memory that the pointer points to It can be all kinds of
int num = 0; // type : int
int arr[10] = {
}; // type : int [10]
int* MuShan = {
1,2,3};
// Remove the name , The rest is the type
Two Definition of pointer
1 Operator
*: Definition time , Indicates that the definition is a pointer Other times, it means resolving references
&: Fetch address ( Used to get the address )
2 Definition
There are many ways to understand the definition of pointers ;
- type Variable name ;
int* p; // type : int
// Variable name : *p
- The type of the pointer itself Pointer name ;
int* p; // The type of the pointer itself : int*
// Pointer name : p
- The type pointed to by the pointer * Pointer name ;
int* p; // The type pointed to by the pointer : int
// Pointer name : p
Back to :
- Remove the pointer name , The rest is the type of the pointer itself
- Remove the pointer name and a *, The rest is the type pointed to by the pointer
int****** p1; // p1 Type of itself : int******
// p1 Type of point : int***** (6 The level pointer can point to 5 Level pointer )
3、 ... and Pointer memory
- All pointers , Whatever the type , Occupy... In memory 4 Bytes of memory ( It's the address )( Specific and 64/32 Bit environment related )
#include <stdio.h>
int main()
{
char* pch;
short* psh;
int* pn;
float* pf;
double* pd;
printf("%d\n", sizeof(pch)); // 4
printf("%d\n", sizeof(psh)); // 4
printf("%d\n", sizeof(pn)); // 4
printf("%d\n", sizeof(pf)); // 4
printf("%d\n", sizeof(pd)); // 4
return 0;
}
- Point to the starting address
int num = 10;
int* p = #
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-naQJWXaU-1644340436740)(C:\Users\admin\Desktop\ Six Star Education \ Study \ The pointer \ The pointer ( One )]\ Pointer basis ( One )1-1.png)
Four Initialization and assignment of pointer
1 Use the address of the corresponding type variable
int num = 10;
int* pn = # // initialization
float f = 3.14f;
float* pf;
pf = &f; // assignment
2 Use the same type of pointer
int num = 10;
int* pn = # // Initial value
int* p1 = pn; // initialization
int* p2;
p2 = pn; // assignment
3 Direct address
int* p = (int*)0x36;
4 Using array names
The first level pointer can accept the array names of a stack of one bit arrays
int arr[5] = {
1,2,3,4,5 };
int* p = arr;
5 character string
#include <stdio.h>
int main()
{
// The array name is the first address of the array
char arr[5] = {
'a','b','c' };
char* p = arr;
printf("%s\n",p); // Output :abc
char str[5] = "abcd";
p = str;
printf("%s\n", str); // Output :abcd
printf("%s\n", p); // Output :abcd
// ==> char* The pointer of type can be directly used to print the whole string to '\0' stop it
const char* p1;
p1 = "1234";
printf("%s\n",p1); // Output :1234
const char* p2 = "Mushan";
printf("%s\n",p2); // Output :Mushan
return 0;
}
6 empty
int* p = NULL;
int* p1 = (int*)0x0;
/* NULL: #define NULL 0 0 Address occasionally , The pointer is defined , But it doesn't point to Or is it , The pointer is used up , There's no point The pointer doesn't know where to point ( Will randomly point to ) Pointer at this point , It's dangerous ( Wild pointer ) therefore Pointers in these cases Arrange an address for them to point to Point to 0 Address */
7 Multi level pointer
#include<stdio.h>
int main() {
int num = 10;
printf(" num = %d\n", num); // num = 10;
printf("&num = %X\n", &num); // &num = 10FFA78
int* p = #
printf("*p = %d\n", *p); // *p = 10 (num value )
printf(" p = %X\n", p); // p = 10FFA78 (num The address of )
printf("&p = %X\n", &p); // &p = 10FFA6C
int** pp = &p; // A secondary pointer
printf("**pp = %d\n", **pp); // **pp = 10 (num value )
printf(" *pp = %X\n", *pp); // *pp = 10FFA78 (num The address of )
printf(" pp = %X\n", pp); // pp = 10FFA6C (p The address of )
printf(" &pp = %X\n", &pp); // &pp = 10FFA60
int*** ppp = &pp; // A three-level pointer
printf("***ppp = %d\n", ***ppp); // ***ppp = 10 (num value )
printf(" **ppp = %X\n", **ppp); // **ppp = 10FFA78 (num Address )
printf(" *ppp = %X\n", *ppp); // *ppp = 10FFA6C (p The address of )
printf(" ppp = %X\n", ppp); // ppp = 10FFA60 (pp The address of )
printf(" &ppp = %X\n", &ppp); // &ppp = 10FFA54
return 0;
}
5、 ... and The addition and subtraction of pointers
The core : The value of the pointer itself ( Point to ) There is no change
Pointer offset
The pointer can add or subtract an integer
Pointer plus or minus an integer , It's actually offset
The range of the offset is an integer unit of addition or subtraction
Company : The number of bytes in memory of the type pointed to by the pointer
The offset : The pointer doesn't change , But you can get the content according to the offset
#include <stdio.h>
int main()
{
int num = 10;
int* p = #
printf("%X\n", p); // EFFB5C
printf("%X\n", p + 1); // EFFB60
return 0;
}
6、 ... and The self increasing and self decreasing of the pointer
Increase and decrease , Will change the pointer to
++: Indicates that the pointer moves back one unit
– : Indicates that the pointer moves forward one unit
Company : The number of bytes in memory of the type pointed to by the pointer
#include <stdio.h>
int main()
{
int num = 10;
int* p = #
printf("%X\n", p); // EFFB5C
printf("%X\n", p + 1); // EFFB60
printf("%d\n",*p); // 10
printf("%X\n", p += 1); // EFFB60
printf("%d\n",*p); // -858993460( meaningless )
return 0;
}
7、 ... and Traverse the array through the pointer
Traversing a one-dimensional array
#include <stdio.h>
int main()
{
int arr[10] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (size_t i = 0; i < 10; i++)
{
printf("%2d", arr[i]);
}
printf("\n");
int* p = arr;
// p and arr, except arr Is outside of a constant , The others are almost the same
for (size_t i = 0; i < 10; i++)
{
printf("%2d", p[i]);
}
printf("\n");
printf("%d\n", p[0]); // 0
printf("%d\n", *(p + 0)); // 0
printf("%d\n", p[1]); // 1( Offset first Post value )
printf("%d\n", *(p + 1)); // 1
// p[n] <==> *(p+n)
return 0;
}
// p[n]: It's called subscript form
// *(p+n): A form called pointer offset
Traversing a two-dimensional array
Two dimensional arrays are also arrays
A two-dimensional array can be regarded as a one-dimensional array whose elements are one-dimensional arrays
The memory of the array is continuous
#include<stdio.h>
int main()
{
int arr[3][4] = {
{
1,2,3,4},
{
5,6,7,8},
{
9,10,11,12}
};
for (size_t i = 0; i < 3; i++)
{
for (size_t j = 0; j < 4; j++)
{
printf("%3d", arr[i][j]);
}
printf("\n");
}
int* p0 = arr[0];
int* p1 = arr[1];
int* p2 = arr[2];
printf("\n");
// 1:
for (size_t i = 0; i < 4; i++)
{
printf("%3d", p0[i]); // 1 2 3 4
}
printf("\n");
for (int i = -4; i <= 7; i++)
{
printf("%3d", p1[i]); // 1 2 3 4 5 6 7 8 9 10 11 12
}
printf("\n");
for (int i = 0; i < 12; i++)
{
printf("%3d", arr[0][i]); // 1 2 3 4 5 6 7 8 9 10 11 12
}
printf("\n");
// Subscript : Ensure that the array does not cross the boundary
// 2:
int* p = &arr[0][0];
for (int i = 0; i < 12; i++)
{
printf("%3d", arr[0][i]); // 1 2 3 4 5 6 7 8 9 10 11 12
}
printf("\n");
for (int i = 0; i < 12; i++)
{
printf("%3d", *p); // 1 2 3 4 5 6 7 8 9 10 11 12
p++;
}
printf("\n");
return 0;
}
边栏推荐
- 河南中创|从云到边,边缘计算如何赋能数据中心
- Some suggestions on writing code to reproduce the paper!
- Machines / scenarios not suitable for webrdp
- 【科普视频】到底什么是透镜天线?
- 我在某大厂做软件测试工程师的《一天完整工作流程》
- JMeter operation process that can be understood at a glance
- The 14th five year plan and investment feasibility study report of global and Chinese hydraulic ester base oil industry 2022 ~ 2028
- I'm fired because I can only test basic functions····
- 打造Flutter高性能富文本编辑器——渲染篇
- The annual salary of testers in large factories ranges from 300000 to 8K a month. Roast complained that the salary was too low, but he was ridiculed by netizens?
猜你喜欢

“還是學一門技術更保險!”杭州校區小哥哥轉行軟件測試,喜提10K+雙休!

感知机从0到1

Defect detection, introduction to Halcon case.

Yixin Huachen talks about how to do a good job in customer master data management

Sharing of Manta network parallel chain solutions by Hufu Research Institute

The CSV used for JMeter performance test is bullshit
![[roe] (2) roe agreement](/img/5a/598da9b140216df046698766de41bb.png)
[roe] (2) roe agreement

The resignation of the chief oracle engineer was furious: MySQL is a terrible database, so it is recommended to consider PostgreSQL!
![Article 7: Design of multifunctional intelligent trunk following control system | undergraduate graduation project - [module device selection, list and data]](/img/9f/4337d5064d9fc93da4c17784a3accc.jpg)
Article 7: Design of multifunctional intelligent trunk following control system | undergraduate graduation project - [module device selection, list and data]

A knowledge map (super dry goods, recommended collection!)
随机推荐
Learn to crawl steadily 07 - detailed explanation of how to use XPath
Intel trimbert: tailor Bert for trade-offs
Defect detection, introduction to Halcon case.
给你一个项目,你将如何开展性能测试工作?
Simulated 100 questions and simulated examination for safety management personnel of metal and nonmetal mines (small open pit quarries) in 2022
Chinese Version Vocaloid AI Tuner Feasibility Test
Perceptron from 0 to 1
Article 5: Design of multi-functional intelligent trunk following control system | undergraduate graduation design - [learning of controller arduino+stm32]
一看就懂的JMeter操作流程
Matlab foundation 04 - detailed analysis of the use and complex application of colon operator ":"
Chapter V - Fund professional ethics
Image retrieval based on cross modal AI model
Data visualization big screen - big screen cloud minimalist user manual
Industry competition analysis and investment scale research report of global and Chinese micro potato industry 2022-2028
A summary of the interface automation test problems most easily encountered
Point cloud perception algorithm interview knowledge points (I)
How to guarantee industrial control safety: system reinforcement
Three times a day (in serial...)
How to access the traifik proxy dashboard using the rancher desktop
[project training] JWT