
数组是一组使用通用名称存储的相关项。
声明数组的语法如下 -
datatype array_name [size];
数组可以通过两种方式初始化,如下 -
数组也可以在声明时初始化,如下所示 -
立即学习“C语言免费学习笔记(深入)”;
int a[5] = {100,200,300,400,500};函数是一个独立的块,用于执行特定的明确定义的任务。将数组作为参数传递给函数的两种方法如下 -
将整个数组作为参数发送给函数。
将各个元素作为参数发送给函数。
现在,让我们了解如何在 C 语言中将整个数组作为参数发送给函数.
要将整个数组作为参数发送,请尝试在函数调用。
要接收整个数组,必须在函数头中声明数组。
请参考下面给出的示例 -
#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); // Sending entire array ‘a’ using array name
getch( );
}
void display (int a[5]) {//receiving entire array
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>
void main(){
//Declaring the array - run time//
int array[5],i;
void rev(int array[5]);
//Reading elements into the array//
printf("Enter elements into the array: ");
//For loop//
for(i=0;i<5;i++){
//Reading User I/p//
printf("array[%d] :",i);
scanf("%d",&array[i]);
}
//Displaying reverse order of elements in the array//
printf("The elements from the array displayed in the reverse order are :");
rev(array); // Sending entire array ‘a’ using array name
getch();
}
void rev(int array[5]){ //receiving entire array
int i;
for(i=4;i>=0;i--){
//Displaying O/p//
printf("array[%d] :",i);
printf("%d",array[i]);
}
}当上述程序一起编译并执行时,会产生以下结果 -
Enter elements into the array: array[0] :23 array[1] :34 array[2] :12 array[3] :56 array[4] :12 The elements from the array displayed in the reverse order are: array[4] :12 array[3] :56 array[2] :12 array[1] :34 array[0] :23
以上就是如何在C语言中将整个数组作为参数发送?的详细内容,更多请关注php中文网其它相关文章!
C语言怎么学习?C语言怎么入门?C语言在哪学?C语言怎么学才快?不用担心,这里为大家提供了C语言速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号