当前位置:网站首页>Pointer array parameter passing, pointer parameter passing
Pointer array parameter passing, pointer parameter passing
2022-07-02 22:25:00 【It's Yi'an】
Catalog
The pointer passes the parameter
Array parameters
One dimensional array
When we pass in a one-dimensional array, we enter the array name , The array name is a pointer to the first element
When receiving the function , Just make sure that the type of the incoming address is the same
#include<stdio.h>
void test(int arr[]) {} //int arr[] Can be seen as int *arr
void test1(int arr[10]){} // What is passed in is the address pointing to the first element , and [] Independent of the value in
void test2(int* arr){} // One int* Pointer to type
int main(void) {
int arr[10] = { 0 };
test(arr); // take arr As a pointer , Point to first element , The type is int*
test1(arr);
test2(arr);
return 0;
}
Two dimensional array
When we pass in a two-dimensional array, we also enter the array name , The array name is an address that points to the first element , But here, the first element is regarded as a one-dimensional array
int arr[3][5] = {
{1,2,3,4,5}, // First element arr It represents the address of the one-dimensional array
{2,3,4,5,6}, // The second element
{3,4,5,6,7} // The third element
};
Same will arr As a pointer , Is a pointer to an array , That is, array pointers , The type is int(*)[5].
When receiving the function , Just make sure that the type of the incoming address is the same
#include<stdio.h>
void test(int arr[3][5]) {} //int arr[][5] Can be seen as int(*)[5]
void test1(int arr[][5]) {} // What is passed in is to point to the first element ( Array ) The address of , And the first [] Independent of the value in
void test2(int(*arr)[5]) {}
//*arr=arr[0],*(arr+i)=arr[i]
//*(arr[i]+j)=arr[i][j]
//*(*(arr+i)+j)=arr[i][j]
// About [] The function of array pointer can be seen in the previous article
int main(void) {
int arr[3][5] = {
{1,2,3,4,5}, // First element arr It represents the address of the one-dimensional array
{2,3,4,5,6}, // The second element
{3,4,5,6,7} // The third element
};
test(arr); // take arr As a pointer , Point to first element ( Array ), The type is int(*)[5]
test1(arr);
test2(arr);
return 0;
}
The pointer passes the parameter
First level pointer
When the first level pointer is passed into the function, it is passed to the address , That's the pointer , When receiving, as long as the type is the same .
#include<stdio.h>
void test(int* p) {} // Both int* Pointer to type
void test1(int p[]) {}
void test2(char* p) {} // Both char* Pointer to type
void test3(char p[]) {}
int main(void) {
int a = 10;
int* p = &a;
test(p);
test1(&a);
char b = 'w';
char* p1 = &b;
test2(p1);
test3(&b);
return 0;
}
The secondary pointer
The function receives a second level pointer , The parameter type is a secondary pointer , As long as it is the address of the element up two levels .
void test(int** p) {} //int** type
int a = 0;
int* p = &a;
int** p1 = &p;
test(&p); // Two levels down the pointer is an element or null
test(p1);
A special secondary pointer
I mentioned it in the array pointer before , The array pointer can be regarded as a special secondary pointer , The values of the second level and the first level are the same .
#include<stdio.h>
void test(int** p) {}
void test1(int(*p)[]){} // there 10 Normal should add , In order to demonstrate the secondary pointer feature, there is no
int main(void) {
int arr[10] = { 0 };
int(*p)[10] = &arr;
test(&arr);
test(p);
test1(&arr);
test1(p);
return 0;
}
边栏推荐
- Evolution of messaging and streaming systems under the native tide of open source cloud
- Unity3d learning notes 4 - create mesh advanced interface
- Destroy in beforedestroy invalid value in localstorage
- Hanoi Tower problem
- D4: unpaired image defogging, self enhancement method based on density and depth decomposition (CVPR 2022)
- 《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!
- How do I access the kubernetes API?
- pip安装whl文件报错:ERROR: ... is not a supported wheel on this platform
- Summary of the first week of summer vacation
- Pyqt picture decodes and encodes and loads pictures
猜你喜欢
TinyMCE visual editor adds Baidu map plug-in
《Just because》阅读感受
Oriental Aesthetics and software design
Daily book -- analyze the pain points of software automation from simple to deep
pip安装whl文件报错:ERROR: ... is not a supported wheel on this platform
Daily book - low code you must understand in the era of digital transformation
Official announcement! The golden decade of new programmers and developers was officially released
地理探测器原理介绍
Introduction to the principle of geographical detector
Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring
随机推荐
Sql service intercepts string
sql service 截取字符串
Try to get property'num for PHP database data reading_ rows' of non-object?
Market Research - current market situation and future development trend of marine wet exhaust hose
What "real skills" should a million year old cloud native developer master? Alibaba, Tencent, meituan and byte decrypt together
Web side defense Guide
Market Research - current market situation and future development trend of aircraft front wheel steering system
540. Single element in ordered array
Using emqx cloud to realize one machine one secret verification of IOT devices
Unity3d learning notes 4 - create mesh advanced interface
【剑指 Offer】56 - I. 数组中数字出现的次数
发现你看不到的物体!南开&武大&ETH提出用于伪装目标检测SINet,代码已开源!...
Riding the wind of "cloud native" and stepping on the wave of "digitalization", new programmer 003 starts pre-sale
App page sharing password rails implementation
pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
[Jianzhi offer] 57 And are two numbers of S
[shutter] shutter gesture interaction (small ball following the movement of fingers)
Chargement de l'image pyqt après décodage et codage de l'image
Market Research - current situation and future development trend of environmental friendly fireworks Market
基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习