如何将整个数组作为参数传递给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 好听的元旦简短祝福语