如何将整个数组作为参数传递给C语言中的函数?
大批
数组是一组以通用名称存储的相关项。以下是将数组作为参数传递给函数的两种方式-
将整个数组作为参数发送给函数
将单个元素作为参数发送给函数
将整个数组作为参数发送给函数
要将整个数组作为参数发送,只需在函数调用中发送数组名称。
要接收数组,必须在函数头中声明它。
示例1
#include输出结果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]); }
Enter 5 elements 10 20 30 40 50 Elements of the array are 10 20 30 40 50
示例2
让我们考虑另一个例子来了解更多关于将整个数组作为参数传递给函数的信息-
#include输出结果main (){ void number(int a[5]); int a[5], i; printf ("enter 5 elements\n"); 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\n"); for (i=0; i<5; i++) printf("%d\n" , a[i]); }
enter 5 elements 100 200 300 400 500 elements of the array are 100 200 300 400 500
热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短