一、一维数组传参
#include<stdio.h>
#include<stdlib.h>
#include <stdio.h>
void test(int arr[])//ok? yes
{}
void test(int arr[10])//ok? yes
{}
void test(int *arr)//ok? yes
{}
void test2(int *arr[20])//ok? yes
{}
void test2(int **arr)//ok? yes
{}
int main()
{
//int arr[5];//数组
//int *parr1[10];//指针数组,元素是指针,可以定义指向
//int(*parr2)[10];//数组指针
//int(*parr3[10])[5];//数组指针数组
int arr[10] = { 0 };
int *arr2[20] = { 0 };
test(arr);//看类型是否匹配
test2(arr2);
system("pause");
return 0;
}
传参首先要保证形参和实参的类型要匹配,
在这里笔者重点解释
void test2(int **arr)//ok? yes
{}
int *arr2[20] = { 0 };
二级指针传参:
arr2是数组名,数组名的类型是 int *,指针数组arr2,的类型就是int **,所以可以用二重指针去接收。
#include <stdio.h>
#include<stdlib.h>
void changeAddress(int** value);
int main() {
int arr[] = { 1, 2, 3 };
int* p = arr;
// 改变之前 p 的地址
printf("%p\n", p);
// q 用来保存改变之前 p 的地址
int* q = p;
changeAddress(&p);
// 改变之后 p 的地址
printf("%p\n", p);
printf("%d\n", q[0]);
// 地址改变的差值
printf("%lu\n", p - q);
system("pause");
return 0;
}
void changeAddress(int** value) {
*value += sizeof(int);
}
//没错, 如果说一级指针是用于对数据的更新, 那么二级指针就是用于对数据地址的更新
//以此类推, 三级指针就是对数据地址的地址的更新…