
数组
数组是一组具有相同名称的相关项。以下是将数组作为参数传递给函数的两种方式:
- 将整个数组作为参数传递给函数
- 将单个元素作为参数传递给函数
将整个数组作为参数传递给函数
示例1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # include <stdio.h>
main (){
void display (int a[5]);
int a[5], i;
clrscr();
printf ( "enter 5 elements" );
for (i=0; i<5; i++)
scanf( "%d" , &a[i]);
display (a);
getch( );
}
void display (int a[5]){
int i;
printf ( "elements of the array are" );
for (i=0; i<5; i++)
printf( "%d " , a[i]);
}
|
登录后复制
输出
1 2 3 4 | Enter 5 elements
10 20 30 40 50
Elements of the array are
10 20 30 40 50
|
登录后复制
示例 2
让我们考虑另一个示例,以了解有关将整个数组作为参数传递给函数的更多信息 -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # include <stdio.h>
main (){
void number(int a[5]);
int a[5], i;
printf ( "enter 5 elements</p><p>" );
for (i=0; i<5; i++)
scanf( "%d" , &a[i]);
number(a);
getch( );
}
void number(int a[5]){
int i;
printf ( "elements of the array are</p><p>" );
for (i=0; i<5; i++)
printf( "%d</p><p>" , a[i]);
}
|
登录后复制
输出
1 2 3 4 5 6 7 8 9 10 11 12 | enter 5 elements
100
200
300
400
500
elements of the array are
100
200
300
400
500
|
登录后复制
以上是如何在C语言中将整个数组作为参数传递给函数?的详细内容。更多信息请关注PHP中文网其他相关文章!