# 在c中传递数组给函数

## 本文概述

``functionname(arrayname);//passing array``

### 声明接收数组作为参数的函数的方法

``return_type function(type arrayname[])``

``return_type function(type arrayname[SIZE])``

（可选）我们可以用下标符号[]定义尺寸。

``return_type function(type *arrayname)``

### C语言将数组传递给函数示例

``````#include<stdio.h>
int minarray(int arr[], int size){
int min=arr[0];
int i=0;
for(i=1;i<size;i++){
if(min>arr[i]){
min=arr[i];
}
}//end of for
return min;
}//end of function

int main(){
int i=0, min=0;
int numbers[]={4, 5, 7, 3, 8, 9};//declaration of array

min=minarray(numbers, 6);//passing array with size
printf("minimum number is %d \n", min);
return 0;
}``````

``minimum number is 3``

## C函数对数组进行排序

``````#include<stdio.h>
void Bubble_Sort(int[]);
void main ()
{
int arr[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
Bubble_Sort(arr);
}
void Bubble_Sort(int a[]) //array a[] points to arr.
{
int i, j, temp;
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Printing Sorted Element List ...\n");
for(i = 0; i<10; i++)
{
printf("%d\n", a[i]);
}
}``````

``````Printing Sorted Element List ...
7
9
10
12
23
23
34
44
78
101``````

## 从函数返回数组

``````int * Function_name() {
//some statements;
return array_type;
}``````

``````#include<stdio.h>
int* Bubble_Sort(int[]);
void main ()
{
int arr[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
int *p = Bubble_Sort(arr), i;
printf("printing sorted elements ...\n");
for(i=0;i<10;i++)
{
printf("%d\n", *(p+i));
}
}
int* Bubble_Sort(int a[]) //array a[] points to arr.
{
int i, j, temp;
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a;
}``````

``````Printing Sorted Element List ...
7
9
10
12
23
23
34
44
78
101``````

• 回顶