当前位置:网站首页>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;
}边栏推荐
- Pyqt picture decodes and encodes and loads pictures
- "Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks!
- Unity3D学习笔记4——创建Mesh高级接口
- Etcd Raft 协议
- VictoriaMetrics 简介
- Official announcement! The golden decade of new programmers and developers was officially released
- Gee: (II) resampling the image
- kubernetes资源对象介绍及常用命令(四)
- [Jianzhi offer] 57 And are two numbers of S
- pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
猜你喜欢

腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?

Hanoi Tower problem

The failure rate is as high as 80%. What should we do about digital transformation?

Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring

Sql service intercepts string

The difference between include < > and include ""

"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba

Pointer and string

An overview of the development of affective computing and understanding research

How to write a good program when a big book speaks every day?
随机推荐
[shutter] shutter resource file use (import resource pictures | use image resources)
20220702-程序员如何构建知识体系?
关于PHP-数据库的 数据读取,Trying to get property 'num_rows' of non-object?
Web侧防御指南
加了定位的文字如何水平垂直居中
sql service 截取字符串
【零基础一】Navicat下载链接
VictoriaMetrics 简介
Oriental Aesthetics and software design
Introduction to victoriametrics
Blue Bridge Cup Winter vacation homework (DFS backtracking + pruning)
Web side defense Guide
【剑指 Offer】57. 和为s的两个数字
[C question set] of V
SQL必需掌握的100个重要知识点:管理事务处理
Market Research - current market situation and future development trend of aircraft front wheel steering system
Service visibility and observability
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
地理探测器原理介绍
Etcd Raft 协议