当前位置:网站首页>[C Advanced] pointer array vs array pointer
[C Advanced] pointer array vs array pointer
2022-07-27 13:43:00 【ecember】
Technical nerd , Save the world !
author :@ecember
special column :《 from 0 Start ——C Language 》
To readers : People who believe in miracles are as great as miracles
| Thank you for give the thumbs-up and Focus on , If you need to see my homepage column |
List of articles
1. Preface
The happiest thing in life is to fight , Not success , The most painful thing in life is laziness , Not failure . Hello everyone , Here is ecember. Today we are going to discuss the concept and application of pointer array and array pointer in pointer ( The following results are in VS2022 Chinese compiler ).
2. Character pointer
We know that there is a type of pointer called character pointer, that is char*
int main()
{
char ch = '0';
char* pc = &ch;
*pc = 'a';
printf("%c\n", ch);
return 0;
}
We have to use character pointer to store the address of characters .
Pen test 1
int main()
{
const char* p1 = "abcdef";// Constant string _ And const irrelevant
const char* p2 = "abcdef";// Will not open up another space
char arr1[] = "abcdef";
char arr2[] = "abcdef";// The space opened up by the array is two spaces
if (p1 == p2)
{
printf("p1 == p2\n");
}
else
{
printf("p1 != p2\n");
}
if (arr1 == arr2)
{
printf("arr1 == arr2\n");
}
else
{
printf("arr1 != arr2\n");
}
return 0;
}
What does the above code output ? Here we must know that when we store strings with pointers , It is actually the address of the first character of the stored string , here p1, p2 Point to the same string , Therefore, the contents stored are the same . And we specify Different arrays when , Even if Same content , Opened up in memory The address is different .
3. Pointer array
seeing the name of a thing one thinks of its function , An array of pointers is an array , An array of pointers .
int main()
{
// Shape array
//int arr[10];// Store integer
A character array
//char arr2[5];// Store characters
//int* arr[10];// An array of integer pointers
//char* ch[5];// An array of character pointers
int a = 10;
int b = 20;
int c = 30;
/*int* p1 = &a; int* p2 = &b; int* p3 = &c;*/
int* arr[3] = {
&a,&b,&c };
int i = 0;
for (i = 0; i < 3; i++)
{
printf("%d ", *arr[i]);// Dereference the contents of the array to get the data
}
return 0;
}

application
int main()
{
int arr1[5] = {
1,2,3,4,5 };
int arr2[5] = {
2,3,4,5,6 };
int arr3[5] = {
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("%d ", *(parr[i] + j));
}
printf("\n");
}
return 0;
}
there parr Array is pointer array , The address of the first element of the above three arrays is stored ,parr[ i ] It is equivalent to the array name of each array , I.e. on behalf of The address of the element at the beginning of each line , You can add j To access each element of the array .
4. Array pointer
Array pointers are pointers ? Or arrays ? The answer is : The pointer .
We are already familiar with :
Shaping the pointer : int * pint; A pointer that can point to shaped data . Floating point pointer : float * pf; A pointer that can point to floating-point data . The array pointer should be : can Pointer to array .
What about the basic structure of pointer array ?
int *p1[10];
int (*p2)[10];
//p1, p2 What are the differences ?
explain :p2 The first and * combination , explain p Is a pointer variable , Then point to a size of 10 An array of integers . therefore p It's a pointer , Point to an array , It's called Array pointer .
Pay attention here :[ ] Priority is higher than * The no. , So we have to add () To guarantee p The first and * combination .
For the priority of operators , You can see my blog : Operator details .
int main()
{
int arr[10] = {
0 };
int(*p)[10] = &arr;// Array pointer
//p That is, the array pointer type name ——int(*)[10] Adding one indicates skipping an array
return 0;
}
practice
int main()
{
char* arr[5] = {
0 };
char*(*p)[5] = &arr;
return 0;
}
among p What is it? ? First p The first and * Combine to form a pointer , Then we looked at another one behind [ ] Indicates that it points to a 5 Array of elements , What type of array elements ?——char * , So p Is an array element pointing to char* The pointer to the array of .
Little science popularization
&arr VS arr
What's the difference between the two ?
Usually , The array name we are talking about is the address of the first element of the array
But there are two exceptions :
1.sizeof( Array name ), This represents the entire array ,sizeof It calculates the size of the entire array
2.& Array name , Here is the address of the whole array
Application of array pointer
void print2(int(*p)[5], int c, int r)
{
int i = 0;
for (i = 0; i < c; i++)
{
int j = 0;
for (j = 0; j < r; j++)
{
//p + i Is pointing to the i Yes
//*(p + i) It's equivalent to getting the third i That's ok , Also equivalent to the second i Row array name
printf("%d ", *(*(p + i) + j));//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} };
// Function print arr Array
print2(arr, 3, 5);// What passed here is equivalent to the line pointer of the first line , That's the address on the first line
return 0;
}
We know that the address of the first line is actually passed by the parameters of the two-bit array , that p + i It is equivalent to getting the No i The address of the line , And then I'll explain the quotation , It's equivalent to getting the array name , namely The address of the first element of each line , Re pass +j Then dereference is to access every element of the two-dimensional array .

5. Conclusion
Here we are , our 《 Pointer array VS Array pointer 》 It's near the end , I will continue to update later C Language related content , Endless learning , It will always be left to those who are prepared . I hope my blog can help you , If you like it, you can give the thumbs-up + Collection Oh , You are also welcome to point out my mistakes in the comment area at any time .
边栏推荐
- JS 模块、闭包应用
- Have you understood these 30 questions of enabling financial risk control plus points
- 利用eBPF探测Rootkit漏洞
- Final solution for high collapse (no side effects)
- 以科技传递温度,vivo亮相数字中国建设峰会
- Data enhancement in image processing
- "Digital economy, science and technology for the good" talk about dry goods
- leetcode——83,24;机器学习——神经网络
- C ftp add, delete, modify, query, create multi-level directory, automatic reconnection, switch directory
- How about the strength of database HTAP
猜你喜欢

What services will the futures company provide after opening an account?

Seata's landing practice in ant International Banking

Jianzhi offer 07 rebuild binary tree -- construct binary tree from middle order and post order traversal sequence

Redis总结:缓存雪崩、缓存击穿、缓存穿透与缓存预热、缓存降级

【C进阶】指针数组 VS 数组指针

Intranet penetration based on FRP -- SSH Remote connection to intranet server with the help of public server

附加:【URLEncoder.encode(待编码字符串, “编码方式“);】(是什么?;我们向cookie中设置值的时候,为什么要使用这个去编码?)(待完善……)

Go language series: how to build a go language development environment?

Verilog's system tasks - $fopen, $fclose and $fddisplay, $fwrite, $fstrobe, $fmonitor

Classification and application of slip rings
随机推荐
浏览器内核模块组成
eBPF/Ftrace
leetcode——83,24;机器学习——神经网络
Interface testing practical tutorial 01: interface testing environment construction
网络异常流量分析系统设计
SCI thesis writing
Dichotomy queries values in an array
Conditions and procedures of futures account opening
Verilog's system tasks - $fopen, $fclose and $fddisplay, $fwrite, $fstrobe, $fmonitor
evutil_make_internal_pipe_: pipe: Too many open files
V-on basic instruction
责任链模式在转转精准估价中的应用
MySQL高可用实战方案——MHA
Rotation chart
2. Citrix virtual apps and desktops 2203 clipboard redirection policy
uniapp防止连续点击出错
opencv图像的缩放平移及旋转
Preliminary discussion on NetGen and Gmsh mesh generation of any multiple sub models of CAD based on osg+occ
js基础知识整理之 —— 数组
JS module, closure application