当前位置:网站首页>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
边栏推荐
- 另类实现 ScrollView 下拉头部放大
- Cisco ASA、FTD和HyperFlex HX的漏洞分析复现
- 逆向思维-小故事
- Minorgc, majorgc, fullgc
- JVM instructions for four call methods
- 阿里云防火墙配置,多种设置方式(iptables和fireward)
- Leetcode MySQL database topic 176
- JVM method return address
- Pipeline details of IPC (interprocess communication)
- Alternative implementation of Scrollview pull-down header amplification
猜你喜欢

自定义控件之侧滑关闭 Activity 控件

阿里云防火墙配置,多种设置方式(iptables和fireward)

Codeforces Round #659 (Div. 2)

Rikka with Cake(线段树+线段树)

Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning

Force deduction 85 question maximum rectangle

另类实现 ScrollView 下拉头部放大

Nacos registry cluster

Codeforces Round #659 (Div. 2)

A 3D Dual Path U-Net of Cancer Segmentation Based on MRI
随机推荐
Codeforces Round #659 (Div. 2)
图片验证码控件
弧形 View 和弧形 ViewPager
Leetcode MySQL database topic 176
Codeforces Round #652 (Div. 2)
HDU 4578 Transformation(线段树+有技巧的懒标记下放)
完全二叉树的权值 递归做法 ——最后的编程挑战
leetcode MYSQL数据库题目181
Flutter 基础组件之 Text
两个栈的模拟题
Container of the basic component of the flutter
Leetcode MySQL database topic 178
1099 Build A Binary Search Tree (30 分)
URAL1517 Freedom of Choice 【后缀数组:最长公共连续子串】
gSoap例子——calc
1021 Deepest Root (25 分)
Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning
Setinterval, setTimeout and requestanimationframe
在Activity外使用startActivity()方法报错原因与解决办法
Language characteristics