数组参数、指针参数
在写代码的时候难免要把【数组】或【指针】传给函数,那函数的参数该如何设计呢?
一维数组的传参
#include<stdio.h>
/*void test(int arr[])
{
}
void test(int arr[10])
{
}
void test(int*arr)
{
}
void test2(int*arr[20])
{
}
void test2(int**arr)
{
}*/
//以上写法都成立
int main()
{
int arr[10]={0};
int *arr2[20]={0};
test(arr);
test2(arr2);
return 0;
}
二维数组的传参
#include<stdio.h>
void test(int arr[3][5])
{
}
void test1(int arr[][5])
{
}
//void test2(int arr[3][])//err
//void test1(int arr[][])//err
int main()
{
int arr[3][5]={0};
test(arr);//二维数组传参
test1(arr);
test2(arr);
}
注意二维数组的传参只能省略行,不能省略列。
函数指针-指向函数的指针-存放函数地址的一个指针
#include<stdio.h>
int Add(int x,int y)
{
int z=0;
z=x+y;
return z;
}
int main()
{
int a=10;
int b=20;
printf("%d\n",Add(a,b));
printf("%p\n",&Add);
printf("%p\n",Add);
//&函数名和函数名都是函数的地址
int(*pa)(int,int)=Add;
printf("%d\n",(*pa)(2,3));
return 0;
}
两段有趣的代码: