当前位置:网站首页>[C language] explain the initial and advanced levels of the pointer and points for attention (1)
[C language] explain the initial and advanced levels of the pointer and points for attention (1)
2022-07-02 15:06:00 【Ahao_ te】
List of articles
One 、 Necessary pointer foundation
What is a pointer ?
Two points understood by pointer :
- The pointer is the smallest cell number in memory , That's the address
- Verbal pointer , Usually pointer variables , This variable stores the memory address
So the pointer is the address , Verbally speaking, pointer refers to pointer variable .
Such as :
* explain p It's a pointer , Pointer to the variable p Deposit a The address of , therefore p The value of is the address , The type is int*
int a=10;
int *p=&a;
This is the beginning , Then there will be other complicated pointers , understand p What is important .
What is the size of the pointer ? How did it come from ?
therefore , For in 32 Under the platform
#include<stdio.h>
int main()
{
printf("%d\n", sizeof(int*)); //4
printf("%d\n", sizeof(char*)); //4
printf("%d\n", sizeof(float*)); //4
return 0;
}
What is the meaning of pointer type ?
Let's take a look at such an example
A pointer points to a char The address of a type variable , The pointer +1 Point to next char The address of a type variable ,char Type size one byte corresponds to address +1, and int The type is 4 Bytes , So the address +4.( Note that there +1 And the size of the address itself 4/8 Byte independent , Don't confuse )
#include<stdio.h>
int main()
{
int n = 10;
char* pc = (char*)&n;
int* pi = &n;
printf("%p\n", &n); //006FFBB8
printf("%p\n", pc); //006FFBB8
printf("%p\n", pi); //006FFBB8
printf("%p\n", pc + 1); //006FFBB9
printf("%p\n", pi + 1); //006FFBBC
return 0;
}
We know different types of pointers , In the distance the pointer moves , Depends on the address type pointed to by the pointer .
Dereference of pointer
* Represents the dereference operator
Case one This situation " * " Means pa It's a pointer
int a=10;
char* pa=&a;
The second case This situation " * " Indicates to the pointer pa To dereference , take pa Pointer variable stored address dereference , The variable itself representing the corresponding address .
int a=10;
char* pa=&a;
*pa=20;
Here's an example
Yes char * Pointer dereference of type only accesses 1 Bytes , Yes int * Pointer dereference access of type 4 Bytes .
int main()
{
int n = 0x11223344;
printf("%x\n", n);//11223344
char* pc = (char*)&n;
*pc = 0;
printf("%x\n", n);//11223300
int* pi = &n;
*pi = 0;
printf("%x\n", n);//0
return 0;
}
The type of pointer also determines , When dereferencing a pointer, you can operate on several bytes
Why *pc=0 After is 11223300, instead of 00223344, You can see Small end and large end storage
Wild pointer
Concept : The position of the pointer is unknown ( Random 、 incorrect )
#include<stdio.h>
int main()
{
int* p; // Local variable uninitialized , The value is 0xcccccccc, Random value
*p = 20; // uninitialized , Can't dereference , Application error
return 0;
}
Two 、 Advanced pointer
1、 Character pointer
Let's start with the following code
int main()
{
// The pointer points to a single character ,pc save ch Address
char ch = 'w';
char* pc = &ch;
*pc = 'b';
printf("%c\n", ch);//b
// First character a The address of is assigned to p, Constant string cannot be changed, so add const
const char* p = "abcdef";
char arr[] = "abcdef";
//*p = 'w';//err
*arr = 'w';// First element a->w
// encounter ‘\0’ Terminate printing
printf("%s\n", p);//abcdef
printf("%s\n", arr);//wbcdef
return 0;
}
It is worth noting that , For strings , The pointer stores only the address of the first character .
A classic question :
int main()
{
const char* p1 = "abcdef";
const char* p2 = "abcdef";
char arr1[] = "abcdef";
char arr2[] = "abcdef";
//p1==p2 because p1 and p2 All stored are characters a The address of ,
// The constant string is placed in read-only memory ( Read only don't write ), There is no need to have multiple copies of the same
if (p1 == p2)
printf("p1==p2\n");
else
printf("p1!=p2\n");
// You need to create two different arrays The address of the first element is different
if (arr1 == arr2)
printf("arr1==arr2\n");
else
printf("arr1!=arr2\n");
return 0;
}
2、 Pointer arrays and array pointers
Pointer array : There are pointers in the array
Usually in the following form
//[] Priority is greater than *, So first arr1[5] Array , The data type in the array is int*
//arr1 The type is int* [5]
int* arr1[5];
//arr2[5] Array , The data type in the array is int**
int** arr2[5];
Use the pointer array to store the first address of other arrays , Print 2D array
int main()
{
int arr1[] = {
1,2,3,4,5 };
int arr2[] = {
2,3,4,5,6 };
int arr3[] = {
3,4,5,6,7 };
int* parr[3] = {
arr1,arr2,arr3 };
int i = 0;
for (i = 0; i < 3; i++)
{
int j = 0;
for (j = 0; j < 5; j++)
{
printf("%d ", *(*(parr + i) + j));
}
printf("\n");
}
}
Array pointer : Pointer to array
Usually in the following form
// The pointer parr1 Point to an array , There are 5 Data , The data type is int.
//parr1 The type is int (*)[5]
int (*parr1)[5];
// The pointer parr2 Point to an array , There are 5 Data , The data type is int*
//parr2 The type is int* (*)[5]
int* (*parr2)[5];
Array pointer usually stores the address of the entire array
Be careful : Two special cases of array names
int main()
{
int arr[10] = {
0 };
int (*p)[10] = &arr;
// The array name represents the address of the first element
printf("%p\n", arr); //006FF834
printf("%p\n", arr + 1); //006FF838
// First element address
printf("%p\n", &arr[0]); //006FF834
printf("%p\n", &arr[0] + 1); //006FF838
// The array address represents the address of the entire array
printf("%p\n", &arr); //006FF834
printf("%p\n", &arr + 1); //006FF85C
//sizeof(arr) Only the array name represents the size of the entire array , Note that only
int sz = sizeof(arr);
// This represents the address size
int sz1 = sizeof(arr+0);
printf("%d\n", sz);//40
printf("%d\n", sz1);//4
return 0;
}
1.sizeof( Array name ) The size of the entire array
2.& Array name , The array name here still represents the whole array ,& The array name represents the address of the entire array
Array pointer to array , Then the array pointer should store the array address :
// Array pointer p Point to the address of the first row of the two-dimensional array
void print2(int(*p)[5],int r, int c)
{
int i = 0;
for (i = 0; i < r; i++)
{
int j = 0;
for (j = 0; j < c; j++)
{
//p+i Select which row ,*(p+i) It can be understood that dereferencing a one-dimensional array address becomes the first element address represented by a one-dimensional array name
printf("%d ", *(*(p + i) + j));
}
printf("\n");
}
}
int main()
{
int arr[3][5] = {
1,2,3,4,5,2,3,4,5,6,3,4,5,6,7};
print2(arr, 3, 5); // The two-dimensional array name represents the address of the first row of the array .
printf("%p\n", arr);//00EFFB18
printf("%p\n", arr + 1);//00EFFB2C Add 20 arr Represents the address of the first row array ,+1 Span a one-dimensional array
printf("%p\n", *arr + 1);//00EFFB1C Add 4 *arr Represents the address of the first element in the first row of the array ,+1 Span a number
}
Let's see Array pointer and Pointer array Put together .
// First look in brackets ,arr1[5] Represents an array , The array type is int (*)[10], The storage of this array 5 A pointer to the ,
// Each pointer points to a containing 10 individual int An array of types .
int (*parr1[5])[10]; // An array that holds array pointers
3、 Function parameters and pointer parameters
1、 One dimensional array parameters
One dimensional arrays have the following parameter transfer methods
void test(int arr[])
{
}
void test(int* arr)
{
}
void test(int arr[10])
{
}
void test2(int *arr[20])
{
}
// In the pointer array, there are primary pointers , The array name is the first element address , The second level pointer stores the address of the first level pointer
void test2(int **arr)
{
}
int main()
{
int arr[10] = {
0 };
int* arr2[20] = {
0 };
test(arr);
test2(arr2);
return 0;
}
2、 Two dimensional array parameters
Two dimensional arrays are worth noting , Generally, the array name expresses the address of the first row of one-dimensional array
void test(int arr[3][5])// It can be written like this
{
}
void test(int arr[][])// You can't the second [] The number of cannot be omitted
{
}
void test(int arr[][5])// It can be written like this
{
}
void test(int* arr) // You can't A one-dimensional array cannot represent a two-dimensional array
{
}
void test(int* arr[5]) // You can't This is a pointer array and cannot represent
{
}
void test(int (*arr)[5]) // Sure Array pointer stores an array address
{
}
void test(int **arr) // You can't The second level pointer stores the first level pointer address
{
}
int main()
{
int arr[3][5] = {
0 };
test(arr); //arr Represents the address of the first row of one-dimensional array
return 0;
}
3、 First level pointer parameter transfer
The first level pointer is very simple
void print(int *p)
{
}
int main()
{
int arr[10] = {
1,2,3,4,5,6,7,8,9};
int *p = arr;
print(p);
return 0;
}
4、 The secondary pointer transmits parameters
remember : The secondary pointer stores the address of the primary pointer
void test(char **p)
{
}
int main()
{
char c = 'b';
char*pc = &c;
char**ppc = &pc;
char* arr[10];
test(&pc);// Sure
test(ppc);// Sure
test(arr);// Sure arr Represents the address of the first element of the array , The first element is a first level pointer
return 0;
}
4、 A function pointer
First let's see if the address of the function can be expressed in the usual way .
int test(int x,int y)
{
printf("\n");
return x+y;
}
int main()
{
printf("%p\n", test);//001313FC
printf("%p\n", &test); //001313FC
return 0;
}
With the address , How to save it next ?
First, you need a pointer to store the address *(pfun)=&test, Then you must need the type int, Finally, add parameters , The address is saved
int *(pfun)(int,int)=&test;
Look at the following code
int Add(int x, int y)
{
return x + y;
}
int main()
{
//pf The first and * combination , explain pf Is a pointer , The pointer points to a function , The function parameter pointed to is (int, int), The return value type is int.
int (*pf)(int, int) = Add; // Save function address
int ret = (*pf)(2, 3); // Access by pointer
printf("%d", ret);
return 0;
}
5、 summary
This section focuses on , Understanding of pointer variables 、 Notes for array names of one-dimensional and two-dimensional arrays 、 Array pointer and pointer array 、 Pass parameters of arrays and pointers .
The pointer is always C Language is a difficult problem , But it will be very simple to split it into small ones .
The first section of this time first summarizes the foundation of the pointer and the foundation in advanced , The next advanced content needs to be understood by summing up the above content , So it is very important to understand this section well .
边栏推荐
- 关于网页中的文本选择以及统计选中文本长度
- Full of knowledge points, how to use JMeter to generate encrypted data and write it to the database? Don't collect it quickly
- tmall.product.schema.get( 产品信息获取schema获取 ),淘宝店铺上传商品API接口,淘宝商品发布接口,淘宝商品上传API接口,店铺上传接口,oAuth2.0接口
- MathML to latex
- 可视化搭建页面工具的前世今生
- Bit by bit of OpenCV calling USB camera
- Solve the problem that El radio group cannot be edited after echo
- Key points of compilation principle examination in 2021-2022 academic year [overseas Chinese University]
- Some Chinese character codes in the user privacy agreement are not standardized, which leads to the display of garbled codes on the web page. It needs to be found and handled uniformly
- Mavn 搭建 Nexus 私服
猜你喜欢

Mavn 搭建 Nexus 私服

CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E

电脑怎么设置扬声器播放麦克风的声音

Tmall product details interface (APP, H5 end)

C language exercises - (array)

obsidian安装第三方插件——无法加载插件

Factal: Unsafe repository is owned by someone else Solution

buuctf-pwn write-ups (7)

CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E

kityformula-editor 配置字号和间距
随机推荐
Mfc a dialog calls B dialog function and passes parameters
Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting
HUSTPC2022
buuctf-pwn write-ups (7)
SQL 后计算的利器 SPL
Fundamentals of software testing
广州市应急管理局发布7月高温高湿化工安全提醒
大顶堆、小顶堆与堆排序
taobao.logistics.dummy.send( 无需物流发货处理 )接口,淘宝店铺发货API接口,淘宝订单发货接口,淘宝r2接口,淘宝oAu2.0接口
mathjax 入门(web显示数学公式,矢量的)
[apipost] tutorial
Dragonfly low code security tool platform development path
LeetCode 2310. 个位数字为 K 的整数之和
天猫商品详情接口(APP,H5端)
[development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)
kibana 基础操作
geoserver离线地图服务搭建和图层发布
STM32 standard firmware library function name memory (II)
C language exercises - (array)
2021-2022学年编译原理考试重点[华侨大学]