
数组是一组具有相同名称的相关项。以下是将数组作为参数传递给函数的两种方式:
要将整个数组作为参数传递,只需在函数调用中发送数组名称。
要接收一个数组,必须在函数头中声明。
#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); //calling array
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
让我们考虑另一个示例,以了解有关将整个数组作为参数传递给函数的更多信息 -
立即学习“C语言免费学习笔记(深入)”;
#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); //calling array
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]);
}enter 5 elements 100 200 300 400 500 elements of the array are 100 200 300 400 500
以上就是如何在C语言中将整个数组作为参数传递给函数?的详细内容,更多请关注php中文网其它相关文章!
C语言怎么学习?C语言怎么入门?C语言在哪学?C语言怎么学才快?不用担心,这里为大家提供了C语言速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号