当前位置:网站首页>Related problems of pointer array, array pointer and parameter passing
Related problems of pointer array, array pointer and parameter passing
2022-06-29 10:12:00 【On the Pearl River】
Catalog
Catalog
1. Pointer array : An array of pointers , It's an array , It's just that it doesn't store integers 、 Floating point but pointer .
2. Array pointer : A pointer to an array , It's a pointer .
3. The ginseng
Conclusion :
Pointer array : An array of pointers , It's an array , It's just that it doesn't store integers 、 Floating point but pointer
form :int* arr[10] :
Be careful :arr Is and [ ] First combine with * combination , because [ ] The priority ratio * high

Use :
Through the pointer array , We can use one-dimensional array to realize two-dimensional array
#include <stdio.h>
int main()
{
int arr1[] = { 1,2,3,4,5 };
int arr2[] = { 2,2,3,4,5 };
int arr3[] = { 3,2,3,4,5 };
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");
}
return 0;
}Array pointer : A pointer to an array , It's a pointer .
We all know :
Integer pointer : A pointer that can point to shaped data
Floating point pointer : A pointer that can point to floating-point data
Array pointer : A pointer to an array , It's a pointer .
form : int (*p2) [10]; It's a Array pointer , It's an array , The array has 10 Elements , Every element int type ,p2 Is a pointer array minus p2, The rest is p2 The type of ,int(* ) [10]
notes : because p1 Is and [ ] First combine with * combination , because [ ] The priority ratio * high
Use :
Want to use array pointers well , First, master the following contents :
We know that the array name is usually the address of the first element of the array , But there are two exceptions !!
special : The following is the address of the entire array
* sizeof( Put a separate array name ), notes :sizeof(arr+0) Do not put an array name alone
* & Array name ,
Look at the code :(arr It represents the address of the first element of the array , Writing with a pointer array is complicated )
The following cases are less useful , Array pointers are commonly used for 2D and 3D arrays
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9 };
int(*p)[9] = &arr;
int i = 0;
for (i = 0; i < 9; i++)
{
printf("%d ", *(*p + i));
}
return 0;
}
( When the argument is a two-dimensional array name , Formal parameters can be received with array pointers )
Particular attention :
* This topic arr It's an array name , Represents the address of the first element of the array
* The first element of a two-dimensional array is the first row of the two-dimensional array ( Passed by this topic arr, Is the address of a one-dimensional array )
* Receive a pointer , This pointer points to an array , So we can use array pointer to realize
The above code is as follows :
#include <stdio.h>
void print_arr1(int arr1[3][5], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf("%d ", arr1[i][j]);
}
printf("\n");
}
}
void print_arr2(int(*p)[5], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf("%d ", *(*(p + i) + j));
}
printf("\n");
}
}
int main()
{
int arr[3][5] = { 1,2,3,4,5,6,7,8,9,10 };
print_arr1(arr, 3, 5);
print_arr2(arr, 3, 5);
return 0;
}The ginseng
The above involves array parameter passing , In this section, I will summarize the parameters of one-dimensional array 、 Two dimensional array parameters 、 First level pointer parameter transfer 、 The core content of two-level pointer parameter transfer
One dimensional array parameters
①

②

The code is as follows :
void test1(int arr[10])
{}
void test1(int arr[])
{}
void test1(int* p)
{}
int main()
{
// One dimensional array
int arr[10] = { 0 };
test1(arr);
return 0;
}
void test2(int* arr2[20])
{}
void test2(int** p)
{}
int main()
{
// Pointer array pass parameter
int* arr2[20] = { 0 };
test2(arr2);
return 0;
}Two dimensional array parameters :
Array name of two-dimensional array , Represents the address of the first element , It's actually the address on the first line

The code is as follows :
void test(int arr[3][5])
{}
void test(int arr[][5])
{}
void test(int arr[][])
{}
void test(int (*arr)[5])
{}
void test(int* arr)
{}
void test(int* arr[5])
{}
void test(int** arr)
{}
int main()
{
int arr[3][5] = { 0 };
test(arr);
return 0;
}First level pointer parameter transfer :
Take a different perspective , If the argument part of a function is a pointer , How to write the arguments

The code is as follows :
void print(int* p)
{}
int main()
{
int a = 10;
int* ptr = &a;
int arr[10] = { 0 };
print(&a);
print(ptr);
print(arr);
return 0;
}The secondary pointer transmits parameters :
Another angle , If the formal parameter of a function is a second-order pointer , How to write the arguments

The code is as follows :
void test(int** p)
{}
int main()
{
int* p1;
int** p2;
int* arr[10];
test(&p1);
test(p2);
test(arr);
return 0;
}Conclusion :
If it helps you ,
Don't forget it give the thumbs-up + Focus on Oh , Crab
If it helps you ,
Don't forget it give the thumbs-up + Focus on Oh , Crab
If it helps you ,
Don't forget it give the thumbs-up + Focus on Oh , Crab
边栏推荐
- Language characteristics
- Using rancher to build kubernetes cluster
- Sublime Text3 set to run your own makefile
- Perfect binary tree, complete binary tree, perfect binary tree
- Nacos environmental isolation
- Leetcode MySQL database topic 178
- RecyclerView刷新闪烁与删除Item时崩溃问题
- 在Activity外使用startActivity()方法报错原因与解决办法
- leetcode MYSQL数据库题目178
- Flutter 基础组件之 Image
猜你喜欢

Force deduction 85 question maximum rectangle

使用Rancher搭建Kubernetes集群

六度空间 bfs

The collapsing "2.3 * 10 = 22" produced by multiplying float and int

Sixteen system counter and flow lamp

JVM之虚拟机栈之动态链接

Flutter 基础组件之 ListView

Judgment of points inside and outside polygon

Gmail:如何快速将邮件全部已读

Dynamic linking of virtual machine stack of JVM
随机推荐
阿里云防火墙配置,多种设置方式(iptables和fireward)
单片机集成开发环境Keil5的使用
Leetcode MySQL database topic 181
Leetcode MySQL database topic 176
Flutter 基础组件之 Image
A 3D Dual Path U-Net of Cancer Segmentation Based on MRI
L2-3 这是二叉搜索树吗?-题解超精彩哦
Symphony tutorial
Flutter 基础组件之 ListView
2019.11.13训练总结
Constructing SQL statements by sprintf() function in C language
Reverse thinking - short story
Wandering --- 最后的编程挑战
520 钻石争霸赛 2021
Introduction to intranet penetration tool FRP
2019.11.20训练总结
JVM之对象的内存布局
JVM之TLAB
leetcode MYSQL数据库题目180
manacher